我有三个主要的表
categories
:
categoryID
(主键)CategoryName
Description
。 OrderDetails
OrderID
(主键)ProductID
Quantity
Products
productId
(主键)ProductName
CategoryID
现在我必须编写一个查询来获取特定类别的产品数量?
这是我的尝试
SELECT Products.ProductID,
Products.CategoryID,
SUM(OrderDetails.Quantity)
FROM Products
LEFT INNER JOIN OrderDetails
ON (SELECT OrderDetails.Quantity,
orderDetails.ProductID,
categories.categoryName
FROM orderDetails
LEFT INNER JOIN Categories
ON categories.categoryID=products.CategoryId
)
ON Products.ProductId = OrderDetaqils.ProductId
我没有得到任何接近我的要求的答案。不正确 请尽可能给我解决方案
如果你能给我解决方案,那将是很有帮助的。如果您需要任何其他详细信息,请告诉我
答案 0 :(得分:0)
这是SQL查询
SELECT Product.ProductID,
sum(Quantity) as TotalSoldAmount,
categories.CategoryID,
CategoryName
FROM OrderDetails
-- INNER JOIN over all 3 Tables
INNER JOIN Product ON Product.ProductID = OrderDetails.ProductID
INNER JOIN categories ON categories.CategoryID = Product.CategoryID
-- we need to group our result because we used sum()
GROUP BY categories.CategoryID,
CategoryName,
Product.ProductID
和sqlfiddle作为证明