SELECT
pro.`Name`,
pro.idProduct,
COUNT(DISTINCT pur.idPurchase) AS Sales
FROM
product AS pro
INNER JOIN purchase pur ON pur.idProduct = pro.idProduct
WHERE pro.idUser = '54c26fdac8f5c'
AND COUNT(DISTINCT pur.idPurchase) > '2'
GROUP BY pro.idProduct
我试图获取所有购买量大于2的产品,所以我在where子句中尝试添加此组功能
COUNT(DISTINCT pur.idPurchase) > '2'
但是我得到这个错误
无效使用组功能
最佳答案
尝试这个:
SELECT
pro.`Name`,
pro.idProduct,
COUNT(DISTINCT pur.idPurchase) AS Sales
FROM
product AS pro
INNER JOIN purchase pur ON pur.idProduct = pro.idProduct
WHERE pro.idUser = '54c26fdac8f5c'
GROUP BY pro.`Name`, pro.idProduct
HAVING COUNT(DISTINCT pur.idPurchase) > '2'