mysql left join错误写法

mysql left join错误写法

把left join 右边的表,在where中进行过滤,即使是使用不等于<>操作符,当右表不存关联的纪录时,会过滤掉左表的值,
总结来说,就是当把left join右边的表,写入where的条件时,能查询出纪录,只有满足下面条件
1.left join 左边和右边都存在相应的关联纪录
2.where 条件过滤掉left join 左边和右边表设置的条件

有一行纪录
SELECT t1.*,t2.name
from  tableA         t1
left outer join
      tableB         t2
on t1.id=t2.id
where t1.id='123'  

没有纪录
select * from  tableB where id = '123'

错误的写法:没有纪录
SELECT t1.*,t2.name
from  tableA         t1
left outer join
      tableB    t2
on t1.id=t2.id
where t1.id='123' and t2.type <> 'man'

正确的写法:有一行纪录
SELECT t1.*,t2.name
from  tableA         t1
left outer join
      (select id,name from tableB  where type <> 'man') t2 on t1.id=t2.id
on t1.id=t2.id


转载于:https://my.oschina.net/forrest420/blog/667106

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值