当前位置:   article > 正文

SQL - not exists_sql not exists

sql not exists

NOT EXISTS

not exists常用于子查询跟主查询之间,用于条件判断,根据一个条件返回一个布尔值,从而确定下一步操作如何进行,not exists是exists或in的对立面

  • exists可以返回多个值,exists不在乎返回的是什么,只在乎有没有结果集返回
  • in只能返回一个值

story table

story_idstory_namecreate_user_idupdate_user_id
1111112
2222213
33333410
44444510

user table

user_iduser_name
1AAA
2BBB
3CCC
4DDD
select *
from story A 
where not exists(
	select 1 from user B where A.create = B.id
) 
  • 1
  • 2
  • 3
  • 4
  • 5

结果

story_idstory_namecreate_user_idupdate_user_id
44444510
  • 筛选出A表与B表用户id相同的数据记为X,比如首先筛选了story id 为1,2,3的数据
  • 再从A表中筛选出不存在于X数据中的数据,比如story id为4的数据不存在于story id为1,2,3的数据中
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/851885
推荐阅读
  

闽ICP备14008679号