Mysql表
Name | Amount
------------
name1 | 150
name1 | 90
name2 | 100
name2 | 110
name3 | 10
结果需要像
这样的东西Name | SUM(Amount)
------------
name1 | 240
name2 | 210
尝试
SELECT Name, SUM(Amount)
WHERE Amount < 210 AND SUM(Amount) >= 210
GROUP BY Name
但是得到错误SQLSTATE[HY000]: General error: 1111 Invalid use of group function
如果我移除AND SUM(Amount) >= 210
代码
试图思考......我是否需要首先获得SUM(Amount) AS SumAmount
并在同一个查询中而不是AND SUM(Amount) >= 210
使用AND SumAmount >= 210
? ...但在一个查询中有两个SELECT
...
喜欢什么?
SELECT SUM(Amount) AS SumAmount
WHERE Amount < 210
GROUP BY Name
SELECT Name, SUM(Amount)
WHERE SumAmount >= 210
GROUP BY Name
请,建议
答案 0 :(得分:3)
使用HAVING
子句过滤汇总结果,例如SUM()
等
SELECT Name, SUM(Amount)
FROM tableName
WHERE Amount < 210
GROUP BY Name
HAVING SUM(Amount) >= 210