我有一个这样的表: “回合”是种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
答案 0 :(得分:2)
使用条件聚合:
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;