当前位置: 移动技术网 > IT编程>数据库>Oracle > oracle数据库下关键字EXIST,NOT EXISTS使用讲解

oracle数据库下关键字EXIST,NOT EXISTS使用讲解

2018年09月14日  | 移动技术网IT编程  | 我要评论

唐喜成血溅乌纱,魔法火枪团,李天天

exists (sql 返回结果集为真)

not exists (sql 不返回结果集为真)

如下:

表a

id name

1 a1

2 a2

3 a3

表b

id aid name

1 1 b1

2 2 b2

3 2 b3

表a和表b是1对多的关系 a.id => b.aid

select id,name from a where exist (select * from b where a.id=b.aid)

执行结果为

1 a1

2 a2

原因可以按照如下分析

select id,name from a where exists (select * from b where b.aid=1)

--->select * from b where b.aid=1有值返回真所以有数据

select id,name from a where exists (select * from b where b.aid=2)

--->select * from b where b.aid=2有值返回真所以有数据

select id,name from a where exists (select * from b where b.aid=3)

--->select * from b where b.aid=3无值返回真所以没有数据

not exists 就是反过来

select id,name from a where not exist (select * from b where a.id=b.aid)

执行结果为

3 a3

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网