当前位置:网站首页>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;
边栏推荐
- 【网络通信三】研华网关Modbus服务设置
- Flutter set the background color of the statusbar status bar and APP method (AppBar) internal consistent color.
- Design and Implementation of Compiler Based on C Language
- After Effects tutorial, How to adjust overexposed snapshots in After Effects?
- Combinatorics Notes (6) Associative Algebra of Locally Finite Partially Ordered Sets, Möbius Inversion Formula
- 【C语言】LeetCode27.移除元素
- 几款永久免费内网穿透,好用且简单(内网穿透教程)
- 使用 Postman 工具高效管理和测试 SAP ABAP OData 服务的试读版
- SringMVC中个常见的几个问题
- 牛客网刷题(一)
猜你喜欢

每日练习------随机产生一个1-100之间的整数,看能几次猜中。要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”。

20.支持向量机—数学原理知识

Intelligent bin (9) - vibration sensor (raspberries pie pico implementation)

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

After Effects 教程,如何在 After Effects 中调整过度曝光的快照?

2022 Android interview summary (with interview questions | source code | interview materials)

杰理语音芯片ic玩具芯片ic的介绍_AD14NAD15N全系列开发

Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言

新型电信“套路”,我爸中招了!

【pytorch】1.7 pytorch与numpy,tensor与array的转换
随机推荐
Flex布局详解
MySQL common statements
useragent在线查找
Concurrency, Timing and Relativity
【NLP】什么是模型的记忆力!
使用 Postman 工具高效管理和测试 SAP ABAP OData 服务的试读版
Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds
Go record - slice
牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
使用互相关进行音频对齐
TypeError: unhashable type: ‘list‘
The new telecom "routine", my dad was tricked!
Masterless Replication System (3)-Limitations of Quorum Consistency
牛客 HJ3 明明的随机数
组合学笔记(六)局部有限偏序集的关联代数,Möbius反演公式
mysql的备份表的几种方法
LevelSequence源码分析
Handling write conflicts under multi-master replication (3) - Convergence to a consistent state and custom conflict resolution logic
【码蹄集新手村600题】通向公式与程序相结合
【pytorch】1.7 pytorch与numpy,tensor与array的转换