20191225_关于sql中exists和not exists

exists
n. 存在量词(exist的复数)
v. 存在;出现;活着(exist的三单形式)

 

理所当然 not exists 就是不存在

那么 if  exists 就是表示它引导的子句有结果集返回就是真, not exists 表示它引导的子句没有结果返回就是真; 这两种情况, 都是针对的是否有结果返回, 而不管返回的结果是什么;

示例:

select * from  sys_User where id=1

 

 exists表示存在就为true, 下面的示例输出消息aaa

if   exists(select * from  sys_User where id=1)
begin
print 'aaaa'
end

not exists表示不存在为true, 否则为false

if not  exists(select * from  sys_User where id=111111111)
begin
print 'aaaa'  --没有这个id则会输出 , 因为查到的结果集是空
end

exists和in的区别 (另外的 not exists 和not in的区别是一样的)

select * from sys_User where  user_no in (select user_no,[user_name] from sys_User where [user_name] like '%a%')

select * from sys_User where exists (select user_no,[user_name] from sys_User where [user_name] like '%a%')

 

posted on 2019-12-25 21:22  印子  阅读(1211)  评论(0编辑  收藏  举报

导航