一、排序查询
order by
asc:表示升序,默认为升序
desc:表示降序
order by 子句可以支持单个字段、多个字段、表达式、函数、别名
order by子句一般是放在查询语句的最后面,limit子句除外
# 升序 select salary from employees ORDER BY salary asc; select salary from employees ORDER BY salary; #降序 select salary from employees ORDER BY salary desc; # 按表达式排序 select *,salary*12+(1+IFNULL(commission_pct,0)) 年薪 from employees ORDER BY salary*12+(1+IFNULL(commission_pct,0)) asc; # 按别名进行排序 select *,salary*12+(1+IFNULL(commission_pct,0)) 年薪 from employees ORDER BY 年薪 asc; # 按字段长度进行排序 # 函数length()可以计算字段值长度并返回 select *,salary*12+(1+IFNULL(commission_pct,0)) 年薪 from employees ORDER BY length(last_name) asc; # 查询员工信息,先按照工资进行升序,再按照编号进行降序排列【多字段排序】 # 首先会按照salary进行升序排列,如果salary一致的情况下才会将相同工资的员工employee_id进行降序排列 select * from employees ORDER BY salary asc, employee_id desc;