gpt4 book ai didi

Mysql show Name, SUM(Amount) WHERE Amount 小于 210 AND SUM(Amount) 大于或等于 210

转载 作者:行者123 更新时间:2023-11-29 05:27:56 24 4
gpt4 key购买 nike

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

求教

最佳答案

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

SELECT Name, SUM(Amount)
FROM tableName
WHERE Amount < 210
GROUP BY Name
HAVING SUM(Amount) >= 210

关于Mysql show Name, SUM(Amount) WHERE Amount 小于 210 AND SUM(Amount) 大于或等于 210,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17917754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com