Sorting multiple fields in MySQL

北慕城南 提交于 2019-12-30 08:17:33

问题


I have a table with 2 fields DATE and IMPORTANCE. Now I want to sort both these fields in DESCENDING ORDER so that the rows are ordered by IMPORTANCE for EACH DATE. For example, if sorted correct, rows should return like this:

Dec 3, 2010 - 10
Dec 3, 2010 - 10
Dec 3, 2010 - 8
Dec 3, 2010 - 7
Dec 3, 2010 - 3
Dec 3, 2010 - 1

Dec 2, 2010 - 10
Dec 2, 2010 - 9
Dec 2, 2010 - 3

Dec 1, 2010 - 8
Dec 1, 2010 - 5
Dec 1, 2010 - 5
Dec 1, 2010 - 4

Is there an efficient way of accomplishing this with only one query statement?


回答1:


SELECT * FROM yourtable
ORDER BY `DATE` DESC, `IMPORTANCE` DESC



回答2:


You can add as many fields to ORDER BY as you want.

That'd be something like:

SELECT * FROM table ORDER BY `date` DESC, `importance` DESC


来源:https://stackoverflow.com/questions/4416880/sorting-multiple-fields-in-mysql

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