我有模拟考试,其中一个问题是
哪个供应商名称没有发送红色部分?
数据库架构如下
Supplier (SupplierName, City)
Part(PartName, Color, Weight)
Shipment(SupplierName, PartName, Date)
Shipment(SupplierName) is a F.K. onto Supplier(SupplierName)
Shipment(PartName) is a F.K. onto Part(PartName)
我提出的查询是
SELECT S.SupplierName
FROM Supplier as S
JOIN Shipment as SH on S.SupplierName = SH.SupplierName
JOIN Part as P on SH.PartName = P.PartName
WHERE P.Color <> 'Red'
我的问题是,我使用&lt;&gt;正确的方法?
答案 0 :(得分:2)
您是否发现供应商至少提供了一部分不是红色的部件。
他们也可能提供红色部件,即使是与非红色部件相同的货物。
您可以使用
SELECT S.SupplierName
FROM Supplier AS S
WHERE NOT EXISTS (SELECT *
FROM Shipment AS SH
JOIN Part AS P
ON SH.PartName = P.PartName
WHERE S.SupplierName = SH.SupplierName
AND P.Color = 'Red')