当前位置:网站首页>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;
边栏推荐
- The article you worked so hard to write may not be your original
- i.MX6ULL driver development | 33 - NXP original network device driver reading (LAN8720 PHY)
- After Effects 教程,如何在 After Effects 中调整过度曝光的快照?
- [pytorch] 1.7 pytorch and numpy, tensor and array conversion
- 【码蹄集新手村600题】不通过字符数组来合并俩个数字
- Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?
- flyway的快速入门教程
- Mariabackup实现Mariadb 10.3的增量数据备份
- [TypeScript] In-depth study of TypeScript type operations
- 迁移学习——Domain Adaptation
猜你喜欢
2022年Android 面经总结(附含面试题 | 源码 | 面试资料)
Kotlin coroutines: continuation, continuation interceptor, scheduler
selenium的常见方法及使用
EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
【码蹄集新手村600题】通向公式与程序相结合
IP protocol from 0 to 1
【码蹄集新手村600题】不通过字符数组来合并俩个数字
自动化测试—web自动化—selenium初识
Golang 小数操作之判断几位小数点与四舍五入
adb shell error error: device unauthorized
随机推荐
阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
20.支持向量机—数学原理知识
牛客 HJ20 密码验证合格程序
go记录之——slice
【luogu P8326】Fliper(图论)(构造)(欧拉回路)
【Yugong Series】July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
联邦学习:联邦场景下的多源知识图谱嵌入
Golang——从入门到放弃
TestCafe之如何进行调试
【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
使用 Postman 工具高效管理和测试 SAP ABAP OData 服务的试读版
mysql的备份表的几种方法
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
spark报错OutOfMemory「建议收藏」
Masterless Replication System (3)-Limitations of Quorum Consistency
AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)
Premiere Pro 2022 for (pr 2022)v22.5.0
华为手机一键开启“维修模式”隐藏所有数据,让手机隐私更加安全
牛客 HJ16 购物单