语法
select 查询列表
from 表
(where 筛选条件)
order by 排序规则
1.order by 排序列表【asc升序;desc降序;如果不写默认升序】
2.order by 语句通常在最后,limit除外
*/
#例1:工资从高到底查询:
select * from employees order by salary desc;
添加筛选条件
#例2:查询部门编号>=90的员工信息,按入职时间的先后顺序进行排序【添加筛选条件】
select * from employees
where department_id >= 90
order by hiredate asc;
按表达式排序
#例3:按年薪的高低显示员工的信息和年薪【按表达式排序】
select *,salary*12*(1+ifnull(commission_pct,0)) as 年薪
from employees
order by salary*12*(1+ifnull(commission_pct,0)) desc;
按别名排序
#例4:按年薪的高低显示员工的信息和年薪【按别名排序】
select *,salary*12*(1+ifnull(commission_pct,0)) as 年薪
from employees
order by 年薪 desc;
按函数排序
#例5:按姓名的长度显示员工的姓名和工资【按函数排序】length()计算字节长度
select length(last_name),last_name,salary
from employees
order by length(last_name);
按多个字段排序
#例6:先按员工工资排序降序,再按员工编号排序升序【按多个字段排序】
select *
from employees
order by salary desc,employee_id asc;
select *
from employees
order by employee_id asc,salary desc;
#注:以上两个结果不同,顺序在前面的排序条件优先级高

这篇博客详细介绍了MySQL中如何进行排序查询,包括基本语法、添加筛选条件、按表达式、别名、函数及多个字段排序,并给出了多个示例,如按工资、入职时间、年薪、姓名长度等进行排序的方法。
4万+

被折叠的 条评论
为什么被折叠?



