wxylog

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%\')

 

分类:

技术点:

相关文章:

  • 2021-08-03
  • 2021-11-05
  • 2021-11-18
  • 2021-10-30
  • 2021-08-03
  • 2021-10-15
  • 2021-11-05
猜你喜欢
  • 2021-11-28
  • 2021-09-14
  • 2021-09-14
  • 2021-11-18
  • 2021-09-14
  • 2021-09-14
  • 2021-09-24
相关资源
相似解决方案