排序查询
语法
select 查询列表
from 表名
【where 筛选条件】
order by 排序的字段或表达式;
特点
-
asc代表的是升序,可以省略
desc代表的是降序 -
order by子句可以支持 单个字段、别名、表达式、函数、多个字段
-
order by子句在查询语句的最后面,除了limit子句
按单个字段排序
SELECT
*
FROM
employees
ORDER BY
salary DESC;
添加筛选条件再排序
案例:查询部门编号>=90的员工信息,并按员工编号降序
SELECT
*
FROM
employees
WHERE
department_id >= 90
ORDER BY
department_id DESC;
按表达式排序
案例:查询员工信息 按年薪降序
SELECT
*,
salary * 12 * ( 1+IFNULL (commission_pct, 0))
FROM
employees
ORDER BY
salary * 12 * ( 1+IFNULL (commission_pct, 0)) DESC;
按别名排序
案例:查询员工信息 按年薪升序
SELECT
*,
salary * 12 * ( 1+IFNULL (commission_pct, 0)) 年薪
FROM
employees
ORDER BY
年薪 DESC;
按函数排序
案例:查询员工名,并且按名字的长度降序
SELECT
length( last_name ),
last_name
FROM
employees
ORDER BY
length( last_name ) DESC;
按多个字段排序
案例:查询员工信息,要求先按工资降序,再按employee_id升序
SELECT
*
FROM
employees
ORDER BY
salary DESC,
employee_id ASC
作业
查询员工的姓名和部门号和年薪,按年薪降序 按姓名升序
SELECT
last_name,
department_id,
salary * 12 * ( 1+IFNULL ( commission_pct, 0 ) ) 年薪
FROM
employees
ORDER BY
年薪 DESC,
last_name ASC;
选择工资不在8000到17000的员工的姓名和工资,按工资降序
SELECT
last_name,
salary
FROM
employees
WHERE
salary NOT BETWEEN 8000
AND 17000
ORDER BY
salary DESC;
查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按部门号升序
SELECT
*,
length( email )
FROM
employees
WHERE
email LIKE '%e%'
ORDER BY
length( email ) DESC,
department_id ASC;
参考资料
MySQL 基础+高级篇-数据库-sql-尚硅谷:https://www.bilibili.com/video/BV12b411K7Zu?from=search&seid=8849847185312367640