[英]SQL, select field if field doesn't exist in another table
我有兩個表,並且要獲取所有Products.ProductID(如果Images.ProductID中不存在)。
我不太確定該怎么寫。
任何幫助都會很棒。
您幾乎可以將英語句子直接翻譯成SQL:
SELECT * FROM Products p
WHERE NOT EXISTS (SELECT * FROM Images i WHERE i.ProductId=p.ProductId)
select ProductID
from Products
where ProductID not in
(
select distinct ProductID
from images
where ProductID is not null
)
要么
select p.ProductID
from Products p
left join images i on i.ProductID = p.ProductID
where i.ProductID is null
SELECT Products.ProductID
FROM Products
WHERE Productd.ProductID NOT IN (
SELECT Images.ProductID
FROM Images
)
select
productid
from Products
where productid not in (select productid from Images)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.