我有两个表,想要获取所有Products.ProductID,如果它不存在于Images.ProductID中。
我不太确定如何写这个......
任何帮助都会很棒。
答案 0 :(得分:4)
您几乎可以直接将英语句子翻译成SQL:
SELECT * FROM Products p
WHERE NOT EXISTS (SELECT * FROM Images i WHERE i.ProductId=p.ProductId)
答案 1 :(得分:0)
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
答案 2 :(得分:0)
SELECT Products.ProductID
FROM Products
WHERE Productd.ProductID NOT IN (
SELECT Images.ProductID
FROM Images
)
答案 3 :(得分:0)
select
productid
from Products
where productid not in (select productid from Images)