用join实现交集,并集,差集,补集的效果

sql集合运算的关键字

      sql中,对于集合的计算有:并集、差集、交集。
      并集:union,union all
      差集:EXISTS,not in,except
      交集:InterSect
      但是吧,不是每个数据库都支持上面所有的,而且关键字还不一样。就像oracle,差集用minus 。emmm…好难受。

join也可以实现

在这里插入图片描述
      实现这种情况,inner join就可以了。

在这里插入图片描述
      实现这种情况,left join就可以了。

在这里插入图片描述
      实现这种情况,right join就可以了。

在这里插入图片描述
      实现这种情况:

select * 
from a left join b 
on a.id = b.id
where b.id is null

      
在这里插入图片描述
      实现这种情况:

select * 
from a right join b 
on a.id = b.id
where a.id is null

      https://blog.csdn.net/weixin_42845682/article/details/105412597这篇文章里有道题,看完可以实践一下。

发布了48 篇原创文章 · 获赞 36 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/weixin_42845682/article/details/105414734