当前位置:网站首页>Mysql数据库,子查询,union,limit篇
Mysql数据库,子查询,union,limit篇
2022-07-24 18:48:00 【世界尽头与你】
Mysql数据库,子查询篇
1.where中的子查询
案例:查询比最低工资高的员工姓名和薪资
子查询,先查询子查询括号里的,再向上级进行查询
mysql> select ename,sal from emp where sal
-> >
-> (select min(sal) from emp);
+--------+---------+
| ename | sal |
+--------+---------+
| ALLEN | 1600.00 |
| WARD | 1250.00 |
| JONES | 2975.00 |
| MARTIN | 1250.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| TURNER | 1500.00 |
| ADAMS | 1100.00 |
| JAMES | 950.00 |
| FORD | 3000.00 |
| MILLER | 1300.00 |
+--------+---------+
13 rows in set (0.02 sec)
2.from子句后的子查询
from后面的子查询,可以将子查询的查询结果当作一张临时表来看待
案例:找出每个岗位的平均薪资的薪资等级
mysql> select
-> t.*,s.grade
-> from
-> (select job,avg(sal) as avgsal from emp group by job) t
-> join salgrade s
-> on
-> t.avgsal between s.losal and s.hisal;
+-----------+-------------+-------+
| job | avgsal | grade |
+-----------+-------------+-------+
| CLERK | 1037.500000 | 1 |
| SALESMAN | 1400.000000 | 2 |
| MANAGER | 2758.333333 | 4 |
| ANALYST | 3000.000000 | 4 |
| PRESIDENT | 5000.000000 | 5 |
+-----------+-------------+-------+
5 rows in set (0.00 sec)
3.union
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。
MySQL UNION 操作符语法格式:
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
参数
expression1, expression2, ... expression_n: 要检索的列。- tables: 要检索的数据表。
WHEREconditions: 可选, 检索条件。DISTINCT: 可选,删除结果集中重复的数据。默认情况下UNION操作符已经删除了重复数据,所以DISTINCT修饰符对结果没啥影响。- ALL: 可选,返回所有结果集,包含重复数据。
案例:查询工作岗位为MANAGER或者SALESMAN的员工信息(使用union)
mysql> select ename,job from emp where job = 'MANAGER'
-> union
-> select ename,job from emp where job = 'SALESMAN';
+--------+----------+
| ename | job |
+--------+----------+
| JONES | MANAGER |
| BLAKE | MANAGER |
| CLARK | MANAGER |
| ALLEN | SALESMAN |
| WARD | SALESMAN |
| MARTIN | SALESMAN |
| TURNER | SALESMAN |
+--------+----------+
7 rows in set (0.00 sec)
为什么使用
union而不是or逻辑运算?因为union效率更高!
另外,对于表连接来说,如果有恰当的union语句可以操作与之相同的效果,优先使用union操作
union使用注意:
- 查询的两个结果集列数必须相同
- 在Mysql中列的数据类型可以存在差异,但是在Oracle中则不可以
4.limit查询
limit用于限制结果集的长度
后面可以存在一个参数或者两个参数(起始下标,取数据条数)
案例:按照薪资降序取出排名前5的员工信息
mysql> select ename,sal
-> from emp
-> order by sal desc
-> limit 0,5;
+-------+---------+
| ename | sal |
+-------+---------+
| KING | 5000.00 |
| FORD | 3000.00 |
| SCOTT | 3000.00 |
| JONES | 2975.00 |
| BLAKE | 2850.00 |
+-------+---------+
5 rows in set (0.00 sec)
limit在order by之后执行
5.分页
假设每页要访问y条数据,那么第x页的数据就是
limit((x-1)*y,y)
边栏推荐
- OPENGL学习(二)OPENGL渲染管线
- Namespace: cluster environment sharing and isolation
- 知乎上的那些神回复
- 2022杭电多校第二场1009 ShuanQ(数学)
- Easily learn pytoch transfer learning to realize surface defect inspection
- Tcl/tk file operation
- core dump
- Cf. bits and pieces (subset pressing DP + pruning)
- Data analysis of network security competition of national vocational college skills competition digital forensics-a
- OpenGL learning (IV) glut 3D image rendering
猜你喜欢

Ionic4 learning notes 3

Calling startActivity() from outside of an Activity context requires the FLAG_ ACTIVITY_ NEW_ TASK flag

L4l7 load balancing

Convolution neural network receptive field calculation Guide

National vocational college skills competition network security competition -- detailed explanation of Apache security configuration

Analysis of dropout principle in deep learning

FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(下)

FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 1)

Make C #

Ionic4 learning notes 9 -- an east project 01
随机推荐
matplotlib
EasyUI adds row level buttons to the DataGrid
32-bit stack overflow advanced
Inoic4 learning notes 2
9. BOM object?
Analysis of dropout principle in deep learning
Type-C边充边听PD协议芯片
Attack and defense world novice zone PWN
Mysql——》数据类型隐式转换
Easily learn pytoch transfer learning to realize surface defect inspection
线程的生命周周期和基本方法
OPENGL学习(四)GLUT三维图像绘制
L4L7负载均衡
CF lomsat gelral (heuristic merge)
core dump
Date function format conversion
Namespace: cluster environment sharing and isolation
网络安全80端口—-PHP CGI参数注入执行漏洞
epoch,batch_ size
Redis data type