gpt4 book ai didi

mysql - 如何对多行求和?

转载 作者:行者123 更新时间:2023-11-29 06:28:13 27 4
gpt4 key购买 nike

我有一个这样的表:'round' 是一种 id

id  round  kind   count
1 1 1 10
1 1 2 10
1 1 1 20
1 2 1 10
1 2 1 30
2 1 1 15
2 1 1 10
2 2 2 20

我想要这些结果

id  round  kind1Total  kind2Total   roundTotal
1 1 30 10 40
1 2 40 40
2 1 25 25
2 2 20 20

最佳答案

使用条件聚合:

SELECT
id,
round,
SUM(IF(kind=1, count, 0)) AS kind1Total,
SUM(IF(kind=2, count, 0)) AS kind2Total,
SUM(count) AS roundTotal
FROM yourTable
GROUP BY
id,
round
ORDER BY
id,
round;

关于mysql - 如何对多行求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58279148/

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