MySQL排序查询

排序查询

在实际应用中,经常都需要按照某个字段都某种排序都结果,实现语法:

sql 复制代码
select 查询列表 from 表 where 条件 order by 排序字段列表 asc | desc;

案例:查询所有员工信息,要求工资从大到小排列

sql 复制代码
select * from employees order by salary desc; // desc:降序,asc:升序

反过来从小到大排列

sql 复制代码
select * from employees order by salary asc; // asc:是可以省略

案例加上条件oreder by语句是放在条件语句后面的,

查询部门编号大于等于90的员工信息,按照入职时间的先后排序

sql 复制代码
select * from employees where department_id>=90 order by hiredate asc;

案例实现按表达式排序,按年薪的高低显示员工信息

sql 复制代码
select *,salary*12*(1+isnull(commission_pct,0)) as 年薪 from employees oreder by salary*12*(1+isnull(commission_pct,0)) desc;

-----oreder by salary*12*(1+isnull(commission_pct,0)) 太长!可以用别名替代:
-----oreder by 年薪 也是可以的!

案例使用函数来排序,按姓名的长度显示员工信息

sql 复制代码
select *,length(last_name) as 姓名的长度 from employees order by length(last_name) desc;

案例实现多字段排序,查询员工信息,首先用工资高低排序,工资一样的在按员工id大到小排序

sql 复制代码
select * from employees order by salary desc , employee_id desc;
相关推荐
Elastic 中国社区官方博客1 天前
Elasticsearch:构建一个 AI 驱动的电子邮件钓鱼检测
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
德育处主任Pro1 天前
在亚马逊云上解决RDS、MariaDB 与 Aurora MySQL复制延迟实战指南
数据库·mysql·mariadb
l1t1 天前
解决PostgreSQL中找不到uniq函数的错误
数据库·postgresql
墨白曦煜1 天前
深入剖析 Redis 客户端:Sentinel 模式下的“寻址”与“感知”艺术
数据库·redis·sentinel
水上冰石1 天前
harbor使用https之证书生成
服务器·数据库·https
韩zj1 天前
服务器定时备份数据库脚本
服务器·数据库·adb
Cat God 0071 天前
基于 CentOS 7.6 的 MySQL 8.0 主从复制
linux·服务器·mysql·centos
笨蛋少年派1 天前
Maxwell数据变更捕获工具简介
数据库·mysql
谷新龙0011 天前
pg_clickhouse插件,在postgresql中借助clickhouse借用OLAP能力
数据库·clickhouse·postgresql
Hello.Reader1 天前
Flink SQL 的 SET 语句会话参数配置与快速自检(SQL CLI 实战)
数据库·sql·flink