📅  最后修改于: 2023-12-03 14:47:35.214000             🧑  作者: Mango
ORDER BY
语句用于对查询结果进行排序。可以根据一个或多个列的数值、日期、文本等类型进行升序或降序排序。默认按升序排序。
考虑以下 employees
表:
| id | name | age | salary | |----|--------|-----|--------| | 1 | Alice | 25 | 5000 | | 2 | Bob | 32 | 7500 | | 3 | Charlie| 28 | 6000 |
按工资升序排序:
SELECT * FROM employees ORDER BY salary ASC;
结果:
| id | name | age | salary | |----|--------|-----|--------| | 1 | Alice | 25 | 5000 | | 3 | Charlie| 28 | 6000 | | 2 | Bob | 32 | 7500 |
按工资降序排序:
SELECT * FROM employees ORDER BY salary DESC;
结果:
| id | name | age | salary | |----|--------|-----|--------| | 2 | Bob | 32 | 7500 | | 3 | Charlie| 28 | 6000 | | 1 | Alice | 25 | 5000 |
按年龄升序、工资降序排序:
SELECT * FROM employees ORDER BY age ASC, salary DESC;
结果:
| id | name | age | salary | |----|--------|-----|--------| | 1 | Alice | 25 | 5000 | | 3 | Charlie| 28 | 6000 | | 2 | Bob | 32 | 7500 |
ORDER BY
必须放在 SELECT
语句的最后面。ORDER BY age AS a ASC
。以上就是 ORDER BY
的简介和示例,希望对大家有所帮助。