有两个简单例子,以说明 “exists”和“in”的效率问题
1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ;
2) select * from T1 where T1.a in (select T2.a from T2) ;
exists用法简单理解:
1:a,b 关联列为 a.id = b.id,现在要取 a 中的数据,其中id在b中也存在:
select * from a where exists(select 1 from b where a.id = b.id)
=select * from a,b where b.id= a.id
2:或者:现在要取 a 中的数据,其中id在b中 不存在:
select * from a where not exists(select 1 from b where a.id = b.id)
=select * from a, b where b.id != a.id