MySQL sorting multiple columns with different sort order

雨燕双飞 提交于 2019-12-01 03:51:21

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/6757334/mysql-sorting-multiple-columns-with-different-sort-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!