簡體   English   中英

如何從另一個表中不存在列的表中進行選擇

[英]How to select from a table where column not exists in another table

表A.

id    city  
1    koronadal  
2    cebu  
3    manila  

表B.

id    city   
1    cebu  

預期產量:

id    city
1    koronadal  
3    manila  

這是我的查詢:

 Select a.id, a.city from tablea a left join tableb b on a.city = b.city

我得錯了輸出..請幫幫我..

你是正確的方式! 現在你只需要過濾那些匹配的人:

Select a.id, a.city
from tablea a
left join tableb b 
 on a.city = b.city
WHERE b.id is null

LEFT JOINING當連接條件不匹配時,左表,在這種情況下tableA仍將被選中,右表中的所有數據,在本例中為tableB將為NULL 所以你需要做的就是在B表上搜索空值。

另一種方法是使用IN()

SELECT * FROM TableA a
WHERE a.city NOT IN(SELECT city FROM TableB)

另一種方式是EXISTS()

SELECT * FROM TableA a
WHERE NOT EXISTS(SELECT 1 FROM TableB b
                 WHERE a.city = b.city)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM