Sorting multiple fields in MySQL

2019-02-16 13:57发布

问题:

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