MySQL表连接

MySQL表连接

1、内连接 – 求交集

image.png

select t1.id id1,
t2.id id2
from A t1
inner join B t2 on t2.id=t1.id;

select t1.id id1,
t2.id id2
from A t1,B t2
where t1.id=t2.id;

2、左连接 – 求A的全集

image.png

select t1.id id1,
t2.id id2
from A t1
left join B t2 on t2.id=t1.id;

3、左连接 – 实现A-B的差集

image.png

select t1.id id1,
t2.id id2
from A t1
left join B t2 on t2.id=t1.id
where t2.id is null;

4、全连接 – A union B 求合集

image.png

select t1.id id1,
t2.id id2
from A t1
left join B t2 on t2.id=t1.id
union
select t1.id id1,
t2.id id2
from A t1
right join B t2 on t2.id=t1.id;

5、全连接 – 去交集

image.png

select t1.id id1,
t2.id id2
from A t1
left join B t2 on t2.id=t1.id
where t2.id is null
union
select t1.id id1,
t2.id id2
from A t1
right join B t2 on t2.id=t1.id
where t1.id is null;

6、右连接 – B-A 求差集

image.png

select t1.id id1,
t2.id id2
from A t1
right join B t2 on t2.id=t1.id
where t1.id is null;

7、右连接 – 求B的全部

image.png

select t1.id id1,
t2.id id2
from A t1
right join B t2 on t2.id=t1.id;

8、表的笛卡尔积

如果表连接没有带条件,则会产生笛卡尔积
假设A表和B表都是10条记录,且一一对应,这个时候A、B两个表无关联条件下的查询,会产生10*10 100条数据。

select t1.id id1,
t2.id id2
from A t1,B t2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值