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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:33:27  来源:igfitidea点击:

MySQL sorting multiple columns with different sort order

mysqlsql-order-by

提问by MySQL DBA

I have a table in which I have three fields with data type date, intand bigint.

我有一个表,其中包含三个数据类型为dateint和 的字段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 intnumof the max datefollowed by the max bigintnumof the max intnumof the max date

你的查询确实是得到了max date,随后max intnum的的max date随后max bigintnummax intnummax 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 排序。