替代not in 和 in 的办法

在程序中,我们经常会习惯性的使用in和not in,在访问量比较小的时候是可以的,但是一旦数据量大了,我们就推荐使用not exists或者外连接来代替了。
如果要实现一张表有而另外一张表没有的数据时,我们通常会这么写:

select * from table t where t.id not in (select id from table2)

我们可以使用下面的语句代替:

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

select a.* from table1 a left join table2 b on a.id = b.id where b.id is not null;

  

原文地址:https://www.cnblogs.com/deepalley/p/12003283.html