我与三列有关系:ProductName
、CategoryID
和 Price
。我需要选择
仅那些价格
高于给定类别中平均产品价格
的产品。
(例如,当apple
(ProductName
)是fruit
(CategoryID
)时,应该选择它,因为它的价格更高高于平均水果价格)。我该怎么做?
我正在考虑类似的事情,但它显然是错误的,因为我将 Price
与 Price
和 CategoryID
进行比较:
SELECT ProductName, AVG(Price) FROM `products`
WHERE (SELECT AVG(Price)) > (SELECT CategoryID, AVG(Price))
GROUP BY ProductName;
最佳答案
类似这样的事情
SELECT ProductName, Price, CategoryID FROM `products` p
WHERE Price > (SELECT AVG(Price) from `products` where CategoryID = p.CategoryID)
关于mysql - 将 AVG(整数) 与类别的 AVG(整数) 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16984502/