当前位置:网站首页>Mysql的自连接和联合查询
Mysql的自连接和联合查询
2022-07-29 05:06:00 【nianyuw】
自连接
自连接查询
自连接查询,顾名思义,就是自己连接自己,也就是把一张表连接查询多次。而对于自连接查询,可以是内连接查询,也可以是外连接查询。
注意自连接一定要起别名
SELECT 字段列表 FROM 表A 别名A JOIN 表A 别名B ON 条件 ... ;
查询员工及其所属领导的名字
select a.name , b.name from emp a , emp b where a.managerid = b.id;
联合查询
对于union查询,就是把多次查询的结果合并起来,形成一个新的查询结果集。
可以用于一次查询多个不相关的表数据
对于联合查询的多张表的列数必须保持一致,字段类型也需要保持一致。
union all 会将全部的数据直接合并在一起,union 会对合并之后的数据去重
SELECT 字段列表 FROM 表A ...
UNION [ ALL ]
SELECT 字段列表 FROM 表B ....;
将薪资低于 5000 的员工 , 和 年龄大于 50 岁的员工全部查询出来
select * from emp where salary < 5000
union all
select * from emp where age > 50;

union all查询出来的结果,仅仅进行简单的合并,并未去重。
select * from emp where salary < 5000
union
select * from emp where age > 50;
union 联合查询,会对查询出来的结果进行去重处理。

边栏推荐
- P5714 [deep foundation 3. Case 7] obesity
- 向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
- 【文件下载】Easyexcel快速上手
- How does WPS take quick screenshots? WPS quick screenshot method
- [config] configure array parameters
- 2021-10-23
- How to monitor micro web services
- TCP三次握手四次挥手
- Five correlation analysis, one of the most important skills of data analysts
- Reveal installation configuration debugging
猜你喜欢

WPS insert hyperlink cannot be opened. What should I do if I prompt "unable to open the specified file"!

SSM integration, addition, deletion, modification and query

WPS如何进行快速截屏?WPS快速截屏的方法

IOS interview preparation - Online

How to install Office2010 installation package? How to install Office2010 installation package on computer

Stack and queue and priority queue (large heap and small heap) simulation implementation and explanation of imitation function

Sparksql inserts or updates in batches and saves data to MySQL

力扣------对奇偶下标分别排序

Using jupyter (I), install jupyter under windows, open the browser, and modify the default opening address

How does word view document modification traces? How word views document modification traces
随机推荐
What if the office prompts that the system configuration cannot run?
SSM integration, addition, deletion, modification and query
时间序列分析的表示学习时代来了?
MySQL time calculation function
SparkSql批量插入或更新,保存数据到Mysql中
IOS interview preparation - other articles
DataSourceClosedException: dataSource already closed at Mon Oct 25 16:55:48 CST 2021
The song of the virtual idol was originally generated in this way!
excel怎么设置行高和列宽?excel设置行高和列宽的方法
P1009 [noip1998 popularization group] sum of factorials
MySQL regularly calls preset functions to complete data update
Torch.nn.crossentropyloss() details
Solution | get the relevant information about the current employees' highest salary in each department |
荣耀2023内推,内推码ambubk
P5714 [deep foundation 3. Case 7] obesity
1 句代码,搞定 ASP.NET Core 绑定多个源到同一个类
开区网站打开自动播放音乐的添加跟修改教程
[untitled]
How does excel filter out the content you want? Excel table filtering content tutorial
[2022 freshmen learning] key points of the third week