1

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

请指教

4

1 回答 1

3

使用HAVING子句过滤聚合结果,例如SUM(),等。

SELECT Name, SUM(Amount)
FROM   tableName
WHERE  Amount < 210
GROUP  BY Name
HAVING SUM(Amount) >= 210
于 2013-07-29T06:59:17.503 回答