得到查询结果后再排序(从磁盘取数据到内存再排序)
select ...
from ...
where ...
order by a -- 按a字段从小到大
order by a -- 按a字段排序,a相等按b排
-- asc - ascend升序
-- desc - descend降序
order by a asc
order by a desc
order by a desc, b asc
1.薪水从小到大排列
USE hr;
SELECT employee_id,first_name,last_name,salary,job_id,department_id
FROM employees
ORDER BY salary;
2.按部门降序排列,相同部门中,按薪水升序排列
USE hr;
SELECT employee_id,first_name,last_name,salary,job_id,department_id
FROM employees
ORDER BY department_id DESC,salary ASC;
3.薪水大于10000的员工,按姓名排序
USE hr;
SELECT employee_id,first_name,last_name,salary,job_id,department_id
FROM employees
WHERE salary>=10000
ORDER BY first_name,last_name;
注意先过滤后排序,先where(先过滤取数据)后order by(排序)
网友评论