当前位置:网站首页>MySQL---排序与分页
MySQL---排序与分页
2022-07-31 17:00:00 【独角鲸需要水】
排序与分页
排序数据
排序规则
使用order by子句排序
asc(不写的情况,默认为asc):升序
desc:降序
order by子句在select语句的结尾
单列排序
select last_name,job_id,department_id,hire_date
from employees
order by hire_date asc(asc可不写)升序
或
SELECT last_name,job_id,department_id,hire_date
FROM employees
ORDER BY hire_date DESC 降序
多列排序
SELECT last_name,department_id,salary
FROM employees
ORDER BY department_id,salary DESC;
可以使用不在SELECT列表中的列排序。
在对多列进行排序的时候,首先排序的第一列必须有相同的列值,才会对第二列进行排序。如果第
一列数据中所有值都是唯一的,将不再对第二列进行排序。
分页
格式
LIMIT [位置偏移量,] 行数
SELECT *
FROM employees
LIMIT 0,10;
MySQL 8.0中可以使用“LIMIT 3 OFFSET 4”,意思是获取从第5条记录开始后面的3条记录,和“LIMIT
4,3;”返回的结果相同。
拓展
在不同的 DBMS 中使用的关键字可能不同。在 MySQL、PostgreSQL、MariaDB 和 SQLite 中使用 LIMIT 关
键字,而且需要放到 SELECT 语句的最后面。
排序与分页练习
1. 查询员工的姓名和部门号和年薪,按年薪降序 按姓名升序显示
select last_name,department_id,salary * 12 annual_sal
from employees
order by annual_sal desc,last_name asc;
2. 选择工资不在 8000 到 17000 的员工的姓名和工资,按工资降序,显示第21到40位置的数据
SELECT last_name,salary
FROM employees
WHERE salary NOT BETWEEN 8000 AND 17000
ORDER BY salary DESC
LIMIT 20,20;
3. 查询邮箱中包含 e 的员工信息,并先按邮箱的字节数降序,再按部门号升序
select last_name,email,department_id
from employees
where email like '%e%'
order by length(email) desc,department_id asc;
或
SELECT last_name,email,department_id
FROM employees
WHERE email REGEXP '[e]'
ORDER BY LENGTH(email) DESC,department_id ASC;
边栏推荐
- go图书管理系统
- 21.支持向量机—核函数的介绍
- 利用PHP开发具有注册、登陆、文件上传、发布动态功能的网站
- 牛客 HJ19 简单错误记录
- This 985 professor is on fire!After 10 years of Ph.D. supervisor, no one has graduated with a Ph.D.!
- 研发过程中的文档管理与工具
- [7.28] Code Source - [Fence Painting] [Appropriate Pairs (Data Enhanced Version)]
- 【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
- Flutter set the background color of the statusbar status bar and APP method (AppBar) internal consistent color.
- flutter设置statusbar状态栏的背景颜色和 APP(AppBar)内部颜色一致方法。
猜你喜欢
如何识别假爬虫?
EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
Kotlin coroutines: continuation, continuation interceptor, scheduler
Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds
华为手机一键开启“维修模式”隐藏所有数据,让手机隐私更加安全
2022年整理LeetCode最新刷题攻略分享(附中文详细题解)
Introduction of Jerry voice chip ic toy chip ic_AD14NAD15N full series development
阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
关于柱状图的经典画法总结
每日练习------随机产生一个1-100之间的整数,看能几次猜中。要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”。
随机推荐
组合学笔记(六)局部有限偏序集的关联代数,Möbius反演公式
[Network Communication 3] Advantech Gateway Modbus Service Settings
MySQL common statements
牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
21.支持向量机—核函数的介绍
研发过程中的文档管理与工具
useragent怎么获取
flyway的快速入门教程
Combinatorics Notes (6) Associative Algebra of Locally Finite Partially Ordered Sets, Möbius Inversion Formula
利用PHP开发具有注册、登陆、文件上传、发布动态功能的网站
GateWay实现负载均衡
AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)
GP 6 overall architecture study notes
go mode tidy出现报错go warning “all“ matched no packages
牛客 HJ17 坐标移动
Concurrency, Timing and Relativity
入职一个月反思
selenium的常见方法及使用
iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组