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'
之上返回。
相关用法
- MySQL ORD()用法及代码示例
- MySQL ORD方法用法及代码示例
- MySQL OR用法及代码示例
- MySQL OCT方法用法及代码示例
- MySQL OCTET_LENGTH方法用法及代码示例
- MySQL OCT()用法及代码示例
- MySQL OLD_PASSWORD用法及代码示例
- MySQL OCTET_LENGTH()用法及代码示例
- MySQL ROUND()用法及代码示例
- MySQL REPEAT()用法及代码示例
- MySQL POWER()用法及代码示例
- MySQL LEAD() and LAG()用法及代码示例
- MySQL IS_IPV4()用法及代码示例
- MySQL RADIANS方法用法及代码示例
- MySQL VARIANCE方法用法及代码示例
- MySQL WEEK()用法及代码示例
- MySQL TIME_FORMAT方法用法及代码示例
- MySQL CURTIME()用法及代码示例
- MySQL weekofyear()用法及代码示例
- MySQL Convert()用法及代码示例
- MySQL IS NOT用法及代码示例
- MySQL FROM_BASE64()用法及代码示例
- MySQL LEFT方法用法及代码示例
- MySQL UCASE方法用法及代码示例
- MySQL PI()用法及代码示例
注:本文由纯净天空筛选整理自Arthur Yanagisawa大神的英文原创作品 MySQL | ORDER BY。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。