左连接、右连接、外连接、内连接、全连接

表:
test1
id name
1 aaa
2 bbb
3 ccc

test2
id name
1 eee
1 fff
3 ggg


左连接也称左外连接(右连接同理):将左表的所有记录列出,右表中只要符合on条件的,与左表记录相拼合,不符合条件的,填以null值。
例:select t1.*,t2.* from test1 t1 left join test2 t2 on t1.id=t2.id
结果:
id name id name
1 aaa 1 eee
1 aaa 1 fff
3 ccc 3 ggg
2 bbb null null

内连接:根据左连接来说,不会将左表的所有记录列出,不符合条件的,不填null值,只找符合条件的。
例:select t1.*,t2.* from test1 t1 inner join test2 t2 on t1.id=t2.id
结果:
id name id name
1 aaa 1 eee
1 aaa 1 fff
3 ccc 3 ggg

全连接:左连接+右连接,把重复的合并。
例:select t1.*,t2.* from test1 t1 full join test2 t2 on t1.id=t2.id
结果:
id name id name
1 aaa 1 eee
1 aaa 1 fff
3 ccc 3 ggg
2 bbb null null

多表联查:笛卡尔积
例:select t1.*,t2.* from test1 t1,test2 t2
结果:
id name id name
1 aaa 1 eee
1 aaa 1 fff
1 aaa 3 ggg
2 bbb 1 eee
2 bbb 1 fff
2 bbb 3 ggg
3 ccc 1 eee
3 ccc 1 fff
3 ccc 3 ggg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值