我有下表:
表1
+----+-----------+-------+
| id | longitude | score |
+----+-----------+-------+
| 1 | 18 | 10 |
| 2 | 18 | 10 |
| 3 | 19 | 5 |
+----+-----------+-------+
我正在尝试创建一个查询,从 Table1 中选择经度 = 18 的所有数据,但只输出得分总和大于 15 的记录。所以基本上,我正在寻找这个输出:
+----+-----------+-------+
| id | longitude | score |
+----+-----------+-------+
| 1 | 18 | 10 |
| 2 | 18 | 10 |
+----+-----------+-------+
我一直在尝试多个查询,但没有一个能给我我要找的东西,或者它们有一些语法错误。我试过这个:
select * from Table1 where longitude = 18 having sum(score)>15
但它产生了这个结果:
+----+-----------+-------+
| id | longitude | score |
+----+-----------+-------+
| 1 | 18 | 10 |
+----+-----------+-------+
如何格式化查询以输出多条记录而不是一条记录?我尝试过使用连接,但遇到了问题。
最佳答案
您缺少GROUP BY
:
select longitude, sum(score) score_sum
from Table1
where longitude = 18
group by longitude
having sum(score)>15
可以看看here .
关于mysql - 如何为 sum() 输出多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43545754/