left join on and与left join on where的区别

本文探讨了LEFT JOIN ON AND与LEFT JOIN ON WHERE在Oracle与MySQL中的行为差异。通过示例说明,在Oracle中,AND条件在ON子句中会影响返回结果,即使条件不满足也会返回左表记录;而在MySQL中,WHERE子句会改变JOIN操作后的结果集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

left join on and与left join on where的区别 分享

inner jion没这个特殊性,则条件放在on中和where中,返回的结果集是相同的。

tab1:

id psize

1 10

2 20

3 30

表2 tab2:

psize name

10 AAA

20 BBB

20 CCC


oracle 中
select * from tab1 left join tab2 on tab1.psize = tab2.psize and tab1.psize='10'

这里的先查询tab1.psize的结果,然后条件不为真也会返回左表中的记录

1 10 10 AAA
2 20 
3 30
即使时0,也会
select * from tab1 left join tab2 on tab1.psize = tab2.psize and tab1.psize='0'
同:select * from tab1 left join tab2 on (tab1.psize = tab2.psize and tab1.psize='0')
ID     PSIZE  PSIZE NAME
1 10 
2 20 
3 30   

mysql中
select * from tab1 left join tab2 on tab1.psize = tab2.psize and tab1.psize='10'

后面一个条件是不执行的


2
select * from tab1 left join tab2 on (tab1.psize = tab2.psize) where tab2.name='AAA'


先执行where前面的语句放入中间表
select * from tab1 left join tab2 on (tab1.psize = tab2.psize)

1 10 10 AAA
2 20 20 BBB
2 20 20 CCC
3 30 

然后执行where语句
1 10 10 AAA
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值