oracle exists 和 not exists 的用法

查询 一个表存在而另一个表不存在的数据,他们通过编号关联。

select  * from table1  t1 where not exists (select bh from table2 t2  where t1.bh = t2.bh);

上面这个语句的意思是: 查询table1中存在的数据 在 table2中不存在的数据。

select * from table1 t1 where exists (select bh from table2 t2  where t1.bh = t2.bh);

上面这个语句是:查询table1中和table2中都存在的数据。

exists 用于  外表小的情况下。

外表大 而内表小  可以用in ,因为 exists 是先循环外表。

外表小 的情况下可以用 exists.


s


你可能感兴趣的:(程序人生)