left join 和except方法区别和联系

目录

相同点:

left join

 except

不同点


假设有两个表:A客户表 和 B客户表,客户uid是唯一主键

相同点:

查询在A中的客户 但不在B中,也就是图中的阴影部分,left join 和except方法都可以实现

left join

select a.* 
from (select * from A where dt='xxxx') a
left join (select * from B where dt='xxxx') b
on a.uid = b.uid
where b.uid is null

 except

select * from 
(select * from A where dt='xxxx')
except
(select * from B where dt='xxxx');

不同点

except在这种场景比left join 更简便:

假设客户表A中,每个客户有多条记录:比如客户1有借记卡,也有信用卡,客户2只有借记卡

要求查询:只有借记卡的客户,那么我们要做的就是剔除有信用卡的客户,用except就更方便了

select * from 
(select * from A where dt='xxxx')
except
(select * from A where dt='xxxx' and card_type = 'xinyongka');

猜你喜欢

转载自blog.csdn.net/Strive_0902/article/details/131412122