当前位置:网站首页>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;
边栏推荐
- Huawei mobile phone one-click to open "maintenance mode" to hide all data and make mobile phone privacy more secure
- 【C语言】LeetCode27.移除元素
- 牛客 HJ16 购物单
- 动态规划之线性dp(上)
- 你辛辛苦苦写的文章可能不是你的原创
- 【luogu P8326】Fliper (Graph Theory) (Construction) (Eulerian Circuit)
- A common method and the use of selenium
- 使用 Postman 工具高效管理和测试 SAP ABAP OData 服务的试读版
- Concurrency, Timing and Relativity
- Golang 小数操作之判断几位小数点与四舍五入
猜你喜欢

【码蹄集新手村600题】通向公式与程序相结合

研发过程中的文档管理与工具

How C programs run 01 - the composition of ordinary executable files

SringMVC中个常见的几个问题

Huawei mobile phone one-click to open "maintenance mode" to hide all data and make mobile phone privacy more secure

TestCafe总结

Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency

【C语言】LeetCode27.移除元素

上传图片-微信小程序(那些年的坑记录2022.4)

华为顶级工程师历时9年总结的“趣谈网络协议”PDF文档,太强了
随机推荐
ThreadLocal
【网络通信三】研华网关Modbus服务设置
How to install CV2 smoothly in Anaconda
【码蹄集新手村600题】通向公式与程序相结合
【愚公系列】2022年07月 Go教学课程 021-Go容器之切片操作
[Source code analysis] BeanFactory and FactoryBean
【Yugong Series】July 2022 Go Teaching Course 020-Array of Go Containers
这位985教授火了!当了10年博导,竟无一博士毕业!
How to switch remote server in gerrit
研发过程中的文档管理与工具
【pytorch】1.7 pytorch与numpy,tensor与array的转换
server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none 失败
BOW/DOM(上)
认识异常 (看完这篇你就懂了)
go记录之——slice
智能垃圾桶(八)——红外对管传感器(树莓派pico)
研发过程中的文档管理与工具
tensorflow2.0 cnn(layerwise)
21.支持向量机—核函数的介绍
Multi-datacenter operation and detection of concurrent writes