mysql a join a_【MySQL】MySQL的7种join

在上一篇文章中,我们建了两张表:tbl_dept,tbl_emp,并向这两张表插入了一些数据,在这篇文章中,我们借用前面的两张表,研究MySQL的join。

两张表的数据如下

948371988c43cbb61be875f78392d8dd.png

1、A、B两表共有

84bd56f0ebce5de8b3f5cb5a9fe301e9.png

select * from tbl_emp a inner join tbl_dept b on a.deptId = b.id;

f3136cb3b5b9b2c5739355197b2e3679.png

2、A、B两表共有+A的独有

1ed063a3d02740067e83145d4850a196.png

select * from tbl_emp a left join tbl_dept b on a.deptId = b.id;

fc48495f7039c293aa9c65a7c2d9d320.png

3、A、B两表共有+B的独有

62a07b70c59bc170382535087b2fe1e5.png

Select * from tbl_emp a right join tbl_dept b on a.deptid = b.id

4c0c9b646e2471be6ac538176c2afaf6.png

4、A的独有

492ed1170338341fc67886afb97dc116.png

Select * from tbl_emp a left join tbl_dept b on a.deptid = b.id and b.id is null;

7ba6e63a3e9686782363a17da5ff7407.png

5、B的独有

e2536a336728b399bd2a339740d9bf7d.png

select * from tbl_emp a right join tbl_dept b on a.deptid = b.id where a.deptid is null;

8a713ed3ab0d8261364660fd62b7a1a0.png

6、AB全有

23c092ec4b233670c4fcff7397cd83d2.png

select * from tbl_emp a full outer join tbl_dept b on a.deptid = b.id

e6e6022296f8e274616f8c343fe9a253.png

7、A的独有+B的独有

select * from tbl_emp a left join tbl_dept b on a.deptid = b.id where b.id is null

Union

select * from tbl_emp a right join tbl_dept b on a.deptid = b.id where a.deptid is null;

03bf9e0e7ee943948ff89194a174e00e.png

以上是对7中join的总结,具体使用哪种,需要根据具体情况而定。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值