当left outer join 的时候,你应该选择on 还是where

select a.* from a 
left outer join b on a.id = b.aid
left outer join c on a.id = c.cid 
-- and c.code = b.code 

-- where c.code=b.code 

其中 c.code=b.code 在left join的时候回保留a中的空值

但是放到where中 c或者是d的code在left join后会被过滤掉。

当你想保留 b中存在,但是b和c在连接的时候c中为空的值,选择on

当你想过滤掉c中的空值选择where 。

数排查方法:

select a.*

,b.code as bcode

,c.code as ccode 

from a 

left outer join b on a.id = b.aid
left outer join c on a.id = c.cid 
-- and c.code = b.code 

-- where c.code=b.code 

查看 主表的主键,bcode,ccode 两列值。

这是一复杂问题的简单描述,有具体应用的情况,配合数据核查,才能深入理解on和where的区别。

别说我是怎么知道的,我不会告诉你我是查数据并结合业务才理解清楚的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mtj66

看心情

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值