当前位置:网站首页>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 联合查询,会对查询出来的结果进行去重处理。

边栏推荐
- tmux随笔
- EMI interference troubleshooting with near-field probe and current probe
- MySQL time calculation function
- Improve the readability of your regular expressions a hundred times
- How does WPS use smart fill to quickly fill data? WPS method of quickly filling data
- Sguard64.exe ace guard client exe: frequent disk reading and writing, game jamming, and Solutions
- 网安学习-内网安全1
- Activity workflow table structure learning
- Opencv learning 1 (environment configuration)
- [config] configure array parameters
猜你喜欢

学习数据库的第一个程序

时间序列分析的表示学习时代来了?

Google GTEST event mechanism

How is the entered query SQL statement executed?

What if the computer cannot open excel? The solution of Excel not opening

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

ODOO开发教程之透视表

Force deduction ----- sort odd and even subscripts respectively

力扣------对奇偶下标分别排序
![[2022 freshmen learning] key points of the third week](/img/21/6a2c4f7e9114260797497d99b75cd6.png)
[2022 freshmen learning] key points of the third week
随机推荐
Take you to understand JS array
使用Jstack、Jconsole和jvisualvm进行死锁分析
Double type nullpointexception in Flink flow calculation
What if excel is stuck and not saved? The solution of Excel not saved but stuck
传奇服务端如何添加地图
Mysql:the user specified as a definer ('root '@'%) does not exist
Use more flexible and convenient Rogowski coil
< El table column> place multiple pictures
Flink+Iceberg环境搭建及生产问题处理
How does WPS use smart fill to quickly fill data? WPS method of quickly filling data
那个准时上下班,从不愿意加班加点的人,在我前面升职了...
How does excel filter out the content you want? Excel table filtering content tutorial
IOS interview preparation - other articles
Download addresses of various versions of MySQL and multi version coexistence installation
新产品上市最全推广方案
2021-10-11
电脑无法打开excel表格怎么办?excel打不开的解决方法
What if the office prompts that the system configuration cannot run?
Using jupyter (I), install jupyter under windows, open the browser, and modify the default opening address
Use jupyter (2) to establish shortcuts to open jupyter and common shortcut keys of jupyter