当前位置:网站首页>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;
边栏推荐
- BOW/DOM(上)
- 【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
- 2022年必读的12本机器学习书籍推荐
- Flutter set the background color of the statusbar status bar and APP method (AppBar) internal consistent color.
- AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)
- 浅谈网络安全之算法安全
- Smart Trash Can (8) - Infrared Tube Sensor (Raspberry Pi pico)
- Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds
- 仿生毛毛虫机器人源码
- server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none 失败
猜你喜欢
随机推荐
[Source code analysis] BeanFactory and FactoryBean
【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
牛客 HJ19 简单错误记录
Golang 小数操作之判断几位小数点与四舍五入
仿生毛毛虫机器人源码
Verilog实现占空比为5/18的9分频
【pytorch】1.7 pytorch与numpy,tensor与array的转换
牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
基于WPF重复造轮子,写一款数据库文档管理工具(一)
复杂高维医学数据挖掘与疾病风险分类研究
智能垃圾桶(九)——震动传感器(树莓派pico实现)
Flink_CDC搭建及简单使用
TestCafe之如何进行调试
研发过程中的文档管理与工具
Introduction of Jerry voice chip ic toy chip ic_AD14NAD15N full series development
并发性,时间和相对性
GP 6 overall architecture study notes
2022年整理LeetCode最新刷题攻略分享(附中文详细题解)
【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
[TypeScript] In-depth study of TypeScript type operations