sql exists用法(exists用法)

莘全琦
导读 大家好,乐天来为大家解答以下的问题,关于sql exists用法,exists用法这个很多人还不知道,现在让我们一起来看看吧!1、exists可以说是orac...

大家好,乐天来为大家解答以下的问题,关于sql exists用法,exists用法这个很多人还不知道,现在让我们一起来看看吧!

1、exists可以说是oracle数据库开发中比较常见的用法,用exists可以提高sql的效率,可以取代in。

2、比如   a,b 关联列为 a.id = b.id,现在要取 a 中的数据,其中id在b中也存在:select * from a where exists(select 1 from b where a.id = b.id)现在要取 a 中的数据,其中id在b中   不存在:select * from a where not exists(select 1 from b where a.id = b.id)用法详解exists是判断exits后面的sql语句是否为真,若为真则整个sql句子成立,否则没有任何记录。

3、例子:select 1 from dual where exists (select 1 from dual where 2=1);上面的情况肯定是没有记录。

4、select 1 from dual where exists (select 1 from dual where 1=1);上面的情况是有记录返回的。

5、判断另外的表中是否包含某个表的外键记录。

6、例子:select * from table_test awhere exists (select 1 from scott.carol_tmp where pps_master=a.pps_master);这个sql是要检查table_test中的pps_master是否在carol_tmp中。

7、其实用in也可以实现同样的效果,但是in的话效率要低些,特别是碰上一些大表。

8、用exists和in的性能就体现出来了。

本文分享完毕,希望对大家有所帮助。

标签:

免责声明:本文由用户上传,如有侵权请联系删除!