我正在學習 sql (postgres) 并且 id 喜歡查找不存在的值。
我有一個表,表 1 帶有 id,我想找到那些不在表 4 中的 id。
我必須在 3 個表之間加入,因為表 1 包含 id 和表 4 contact_id(不是同一個數字)
表 2,3 需要連接,因為它連接了 id。
那么我如何用“不存在”來做到這一點?
Select t1.id, table4.contact_id
From table1 t1
Join table2 using(id)
Join table3 using(id)
Join table4 using(contact_id)
Where not exists (
Select 1
From table4
Where table4.contact_id=t1.id
);
它不回傳任何值,但應該沒有錯誤訊息...
我認為我有思維錯誤
uj5u.com熱心網友回復:
您的查詢可能不回傳任何值,因為您加入table4,contact_id然后在WHERE子句中排除來自此聯接的行。
要查找不存在的值,通常可以使用LEFT JOINorRIGHT JOIN或FULL OUTER JOIN然后過濾子句中具有NULL值的行。WHERE
試試這個 :
SELECT t1.id
FROM table1 t1
LEFT JOIN table2 t2 using(id)
LEFT JOIN table3 t3 using(id)
LEFT JOIN table4 t4 using(contact_id)
WHERE t4.contact_id IS NULL
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/411526.html
標籤:
