posts - 139,comments - 4,views - 61208
一、排序查询
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;
复制代码

 

 
posted on   时光以北暮南城  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示