开发者

MySQL sorting multiple columns with different sort order

开发者 https://www.devze.com 2023-03-21 19:48 出处:网络
I have a table in which I have three fields with data type date, int and bigint. I want to sort my select query using all these three columns. I want to sort them all in descending order. For example

I have a table in which I have three fields with data type date, int and 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.


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

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

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


It is not possible to get a result starting from max of all three columns. like latest date, highest intnum and higest 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号