GROUP by和WHERE不能一起工作

时间:2012-12-20 16:54:26

标签: mysql mysql-error-1064

为什么此代码有效:

SELECT year, sum(value)
FROM test
GROUP BY year

这不是吗?

SELECT year, sum(value)
FROM test
GROUP BY year
WHERE `user` = 1

4 个答案:

答案 0 :(得分:7)

GROUP BY需要在WHERE之后:

SELECT year, sum(value) FROM test WHERE user = 1 GROUP BY year 

答案 1 :(得分:1)

你试过吗

 SELECT year, sum(value) FROM test WHERE user = 1 GROUP BY year

答案 2 :(得分:1)

我不是专家,但尝试“HAVING”条款:

SELECT year, sum(value)
FROM test
GROUP BY year
having `user` = 1

答案 3 :(得分:0)

反转GROUP BYWHERE条款:

SELECT year, SUM(value)
  FROM test
 WHERE user = 1
 GROUP BY year