在程序中,我们经常会习惯性的使用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;

  

相关文章:

  • 2021-10-01
  • 2022-12-23
  • 2021-08-21
  • 2022-03-08
  • 2022-01-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-11
  • 2021-12-25
  • 2021-11-03
  • 2021-05-21
  • 2022-12-23
  • 2021-11-07
  • 2022-12-23
相关资源
相似解决方案