PHP MySQL 按多列排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13629571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP MySQL Order by multiple columns
提问by David
I am looking to create an order based on multiple columns in my table - all date related. For example: my columns are: fee_due, fee_2_due, fee_3_due- they all contains results of various dates. However, I want to create an order combining all columns, so fee_duemight be 2012-11-03, fee_2_duemight be 2012-11-01and fee_3_duemight be 2012-11-02.
我希望根据表中的多列创建订单 - 所有日期相关。例如:我的列是:fee_due, fee_2_due, fee_3_due- 它们都包含不同日期的结果。但是,我想创建一个组合所有列的订单,因此fee_due可能是2012-11-03,fee_2_due可能是2012-11-01,fee_3_due也可能是2012-11-02。
My query needs to be:
我的查询需要是:
SELECT *
FROM table_name
ORDER BY [date] DESC
... whereby the dates from the 3 columns join to form one order, regardless of what column they are in.
...其中 3 列中的日期连接形成一个订单,无论它们在哪一列。
Hope that makes sense and thanks in advance.
希望这是有道理的,并提前致谢。
回答by Gutenberg
Additionally you can:
此外,您还可以:
SELECT *
FROM table_name
ORDER BY fee_due ASC,fee_2_due DESC,fee_3_due DESC
You can sort each column independently according to your need.
您可以根据需要对每一列进行独立排序。
回答by MaxHD
I used the following to make it work:
我使用以下方法使其工作:
SELECT * FROM table ORDER BY camp1 ASC, camp2 ASC, camp3 DESC
回答by GBD
Have you try this:
你有没有试过这个:
SELECT *
FROM table_name
ORDER BY fee_due,fee_2_due,fee_3_due DESC

