SQL multiple column ordering
I am trying to sort by multiple columns in SQL, and in different directions. column1
would be sorted descending, and column2
ascending.
How can I do this?
ORDER BY column1 DESC, column2
这种排序方式首先按column1
(降序)排序,然后按column2
(升序排序,这是默认值)排序,只要两行的column1
字段相等。
The other answers lack a concrete example, so here it goes:
Given the following People table:
FirstName | LastName | YearOfBirth
----------------------------------------
Thomas | Alva Edison | 1847
Benjamin | Franklin | 1706
Thomas | More | 1478
Thomas | Jefferson | 1826
If you execute the query below:
SELECT * FROM People ORDER BY FirstName DESC, YearOfBirth ASC
The result set will look like this:
FirstName | LastName | YearOfBirth
----------------------------------------
Thomas | More | 1478
Thomas | Jefferson | 1826
Thomas | Alva Edison | 1847
Benjamin | Franklin | 1706
SELECT *
FROM mytable
ORDER BY
column1 DESC, column2 ASC
链接地址: http://www.djcxy.com/p/34268.html
上一篇: ORDER BY&LIMIT
下一篇: SQL多列排序