group子句给出错误的group函数

时间:2015-09-28 13:51:17

标签: mysql count group-by where

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'

但是我收到了这个错误

  

无效使用群组功能

1 个答案:

答案 0 :(得分:0)

试试这个:

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'