當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


MySQL ORDER BY用法及代碼示例


MySQL 的 ORDER BY 子句對檢索到的記錄進行排序,並默認按升序返回它們。如果需要的話,我們可以指定按降序排序。

用法

SELECT column_name(s)
FROM table_name
ORDER BY column1, column2 (DESC);

例子

考慮下表有關一些學生的信息:

student_id

名稱

名字

day_enrolled

年齡

用戶名

1

Sky

Towner

2015-12-03

17

stowner1

2

Ben

Davis

2016-04-20

19

bdavis2

3

Travis

Apple

2018-08-14

18

tapple3

4

Arthur

David

2016-04-01

16

adavid4

5

Benjamin

Town

2014-01-01

17

btown5

可以使用此處的代碼創建上述示例表。

升序

要按 age 升序對學生進行排序:

SELECT * FROM students
ORDER BY age;



+------------+----------+--------+--------------+------+----------+
| student_id | fname    | lname  | day_enrolled | age  | username |
+------------+----------+--------+--------------+------+----------+
|          4 | Arthur   | David  | 2016-04-01   |   16 | adavid4  |
|          1 | Sky      | Towner | 2015-12-03   |   17 | stowner1 |
|          5 | Benjamin | Town   | 2014-01-01   |   17 | btown5   |
|          3 | Travis   | Apple  | 2018-08-14   |   18 | tapple3  |
|          2 | Ben      | Davis  | 2016-04-20   |   19 | bdavis2  |
+------------+----------+--------+--------------+------+----------+

降序

要按 age 降序對學生進行排序:

SELECT * FROM students
ORDER BY age DESC;



+------------+----------+--------+--------------+------+----------+
| student_id | fname    | lname  | day_enrolled | age  | username |
+------------+----------+--------+--------------+------+----------+
|          2 | Ben      | Davis  | 2016-04-20   |   19 | bdavis2  |
|          3 | Travis   | Apple  | 2018-08-14   |   18 | tapple3  |
|          1 | Sky      | Towner | 2015-12-03   |   17 | stowner1 |
|          5 | Benjamin | Town   | 2014-01-01   |   17 | btown5   |
|          4 | Arthur   | David  | 2016-04-01   |   16 | adavid4  |
+------------+----------+--------+--------------+------+----------+

按字母順序

根據學生名字的字母順序對學生進行排序:

SELECT * FROM students
WHERE fname is NOT NULL
ORDER BY fname;



+------------+----------+--------+--------------+------+----------+
| student_id | fname    | lname  | day_enrolled | age  | username |
+------------+----------+--------+--------------+------+----------+
|          4 | Arthur   | David  | 2016-04-01   |   16 | adavid4  |
|          2 | Ben      | Davis  | 2016-04-20   |   19 | bdavis2  |
|          5 | Benjamin | Town   | 2014-01-01   |   17 | btown5   |
|          1 | Sky      | Towner | 2015-12-03   |   17 | stowner1 |
|          3 | Travis   | Apple  | 2018-08-14   |   18 | tapple3  |
+------------+----------+--------+--------------+------+----------+

對多列進行排序

對於具有相同 age 的學生,按照 age 的升序排列學生,然後按照 fname 的降序排列:

SELECT * 
FROM students
WHERE fname is NOT NULL
ORDER BY age, fname DESC;



+------------+----------+--------+--------------+------+----------+
| student_id | fname    | lname  | day_enrolled | age  | username |
+------------+----------+--------+--------------+------+----------+
|          4 | Arthur   | David  | 2016-04-01   |   16 | adavid4  |
|          1 | Sky      | Towner | 2015-12-03   |   17 | stowner1 |
|          5 | Benjamin | Town   | 2014-01-01   |   17 | btown5   |
|          3 | Travis   | Apple  | 2018-08-14   |   18 | tapple3  |
|          2 | Ben      | Davis  | 2016-04-20   |   19 | bdavis2  |
+------------+----------+--------+--------------+------+----------+

當列條目具有重複數據時,對多列進行排序是有意義的(在這種情況下 'Sky''Benjamin' 都是 17 )。對於 'Sky''Benjamin',我們然後根據 fname 的字母降序順序確定它們的順序,這就是為什麽 'Sky''Benjamin' 之上返回。

相關用法


注:本文由純淨天空篩選整理自Arthur Yanagisawa大神的英文原創作品 MySQL | ORDER BY。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。