SQL——minus、union 、interspect


 

 

 

 

minus、union 、interspect

 

 

minus和not in的区别

minus是求差集,可以寻找左边表存在右边表不存在的数据,即左边表减去两表的交集。

 

minus可以实现not in(使用not in注意空值)的所有功能,

例:

select * from sysdba.origin_table o where  o.id   not in (select id from sysdba.destination_table);

(select id from SYSDBA.ORIGIN_TABLE ) minus (select id from SYSDBA.DESTINATION_TABLE);

但是多了一个功能即寻找所有字段的差集

(select * from SYSDBA.ORIGIN_TABLE ) minus (select * from SYSDBA.DESTINATION_TABLE);

假设有这两张表

 

select * from sysdba.origin_table o where o.id not in (select id from sysdba.destination_table);

 

多出来的功能:所有字段的比对:

(select * from SYSDBA.ORIGIN_TABLE ) minus (select * from SYSDBA.DESTINATION_TABLE);

其实想想也容易明白,一个叫差集,偏重于集合即所有字段的数据,那么交集和in关键字也是一样的道理。

 

为什么不建议使用外键?

https://zhuanlan.zhihu.com/p/62020571

 

posted @ 2021-04-16 14:14  Eric-Shen  阅读(475)  评论(0编辑  收藏  举报