当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。