MySQL MySQL以不同的排序顺序对多列进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6757334/
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
MySQL sorting multiple columns with different sort order
提问by MySQL DBA
I have a table in which I have three fields with data type date
, int
and bigint
.
我有一个表,其中包含三个数据类型为date
、int
和 的字段bigint
。
I want to sort my select query using all these three columns. I want to sort them all in descending order. For example:
我想使用所有这三列对我的选择查询进行排序。我想按降序对它们进行排序。例如:
Select * From mytbl
order by date desc,intnum desc, bigintnum desc;
Is it possible that i could get a result starting from max of all three columns. like latest date, highest intnum and higest bigintnum.
我是否有可能从所有三列的最大值开始得到结果。如最新日期、最高intnum 和最高bigintnum。
回答by bash-
no
不
What your query does is get the max date
, followed by the max intnum
of the max date
followed by the max bigintnum
of the max intnum
of the max date
你的查询确实是得到了max date
,随后max intnum
的的max date
随后max bigintnum
的max intnum
的max date
In other words, your query would not return the maximum value of all three columns
换句话说,您的查询不会返回所有三列的最大值
It orders by the date first, then the intnum, then the bigintnum The results would be something like this
它首先按日期排序,然后是 intnum,然后是 bigintnum 结果将是这样的
2011-07-20 12 14
2011-07-20 12 13
2011-07-20 11 16
2011-07-20 10 12
2011-07-19 20 15
2011-07-18 60 30
2011-07-18 50 14
回答by Sharad
It is not possible to get a result starting from max of all three columns. like latest date, highest intnum and higest bigintnum.
不可能从所有三列的最大值开始获得结果。如最新日期、最高intnum 和最高bigintnum。
Select * From mytbl
order by date desc,intnum desc, bigintnum desc;
As you know what ORDER BY does, if you have multiple columns in order by clause, It will first order by DATE Desc then for the very first Date it will order by INTNUM Desc and then order by BIGINTNUM.
正如您知道 ORDER BY 的作用,如果 order by 子句中有多个列,它将首先按 DATE Desc 排序,然后对于第一个 Date 它将按 INTNUM Desc 排序,然后按 BIGINTNUM 排序。