过滤掉在MySQL中使用GROUP BY获得的结果

时间:2014-09-14 17:55:37

标签: mysql group-by

我有这个问题:

SELECT name, SUM(p.column1+p.column2+p.colum3) AS PICKS
FROM table AS p
WHERE date = '2014-09-10'
GROUP BY name

此查询非常有效。我希望过滤掉p.column1+p.column2+p.column3 = 0

中的结果

我试过了:

SELECT name, SUM(p.column1+p.column2+p.colum3) AS PICKS
FROM table AS p
WHERE PICKS > 0
AND date = '2014-09-10'
GROUP BY name

但它一直告诉我GROUP BY的使用不正确。

2 个答案:

答案 0 :(得分:1)

使用having子句过滤掉聚合函数的结果

SELECT name, SUM(p.column1+p.column2+p.colum3) AS PICKS
FROM table AS p
WHERE date = '2014-09-10'
GROUP BY name
HAVING PICKS > 0

或者您可以使用其他方式使用子选择

SELECT * FROM
(SELECT name, SUM(p.column1+p.column2+p.colum3) AS PICKS
FROM table AS p
WHERE date = '2014-09-10'
GROUP BY name) t
WHERE PICKS > 0

答案 1 :(得分:1)

您需要使用having语句。不是在哪里。

SELECT name, SUM(p.column1+p.column2+p.colum3) AS PICKS
FROM table AS p
AND date = '2014-09-10'
GROUP BY name
HAVING PICKS > 0

请参阅http://dev.mysql.com/doc/refman/5.0/en/group-by-extensions.html