exists和not exists關鍵字


表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')
 
        
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM