我想编写一个查询,以仅给出购买了所有价格低于5的所有产品的那些客户的姓名。而我的查询给出的是甚至购买了价格低于5的单个产品的所有客户。< / p>
SELECT Customers.CompanyName AS Customers
FROM Customers
INNER JOIN Orders
ON Orders.CustomerID = Customers.CustomerID
JOIN [Order Details]
ON [Order Details].OrderID = Orders.OrderID
JOIN Products
ON Products.ProductID = [Order Details].ProductID
WHERE [Order Details].ProductID IN (
SELECT Products.ProductID FROM Products WHERE Products.UnitPrice < 5
)
答案 0 :(得分:0)
您可以尝试以下操作:
SELECT Customers.CompanyName AS Customers
FROM Customers
INNER JOIN Orders
ON Orders.CustomerID = Customers.CustomerID
JOIN [Order Details]
ON [Order Details].OrderID = Orders.OrderID
JOIN Products
ON Products.ProductID = [Order Details].ProductID
WHERE [Order Details].ProductID IN (
SELECT Products.ProductID FROM Products WHERE Products.UnitPrice < 5)
GROUP BY CompanyName
HAVING COUNT([Order Details].ProductID) = (SELECT Count(Products.ProductID) FROM Products WHERE Products.UnitPrice < 5)
我包括了GROUP BY和Haveing条款,这样您就只能获得购买了单价低于5的所有产品的客户