单列
升序
1 |
select name,sal from emp order by sal asc; |
降序
1 |
select name,hiredate from emp order by hiredate desc; |
多列
- 假设我们希望先按
salary
升序排序,再按emp_name
升序排序
1 2 |
SELECT * FROM employees ORDER BY salary ASC, emp_name ASC; |
特殊
按表达式排序
1 2 3 |
SELECT emp_id, emp_name, salary*1.1 AS adjusted_salary FROM employees ORDER BY adjusted_salary DESC; |
按列别名排序
1 2 3 |
SELECT emp_name, salary AS emp_salary FROM employees ORDER BY emp_salary DESC; |
使用CASE
语句自定义排序
1 2 3 4 5 6 7 |
SELECT emp_name, dept_id, salary FROM employees ORDER BY CASE WHEN dept_id = 1 THEN salary ELSE dept_id END ASC; |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 数据处理函数:多行处理函数10/24
- ♥ 表_约束-主键10/26
- ♥ 数据处理函数:单行处理函数10/24
- ♥ 表操作_条件查询-运算符10/23
- ♥ 存储引擎_介绍 && 选择10/28
- ♥ 表_约束-外键10/27