表A
ID NAME
1    A1
2    A2
3  A3

表B
ID AID NAME
1    1 B1
2    2 B2
3    2 B3

 

SELECT ID,NAME FROM A WHERE EXIST (SELECT * FROM B WHERE A.ID=B.AID)

等价于 select id.name from A where A.ID IN (SELECT B.ID FROM B)
执行结果为
1 A1
2 A2

 

NOT EXISTS 就是反过来
SELECT ID,NAME FROM A WHERE NOT EXIST (SELECT * FROM B WHERE A.ID=B.AID)
执行结果为
3 A3

 

 

应用场景:

--不等于
select * from udf_user u where not exists (
        select * from armc_user_team t where t.user_name=u.username_ and t.team_code='DCCDJP'
        )
        
 --同时存在
 select * from armc_user_team t where t.team_code='a'
 and exists(select * from armc_user_team t1 where t1.user_name=t.user_name and t1.team_code='b')
 
        
 

 

相关文章:

  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
  • 2021-10-18
  • 2021-05-08
  • 2021-09-14
  • 2021-11-18
猜你喜欢
  • 2022-12-23
  • 2018-03-26
  • 2022-12-23
  • 2021-08-10
  • 2021-11-28
相关资源
相似解决方案