我有一个表格,收集每周足球比赛的结果。
根据每场比赛的结果,我根据 ID 为玩家输入 3、1 或 0 分,具体取决于他们是赢、平还是输。
id Name A1 B1 C1 A2 B2 C2
1 C 0 3 1 3 0 0
2 G 3 0 1 3 0 0
3 k 3 0 1 0 3 3
4 S 3 0 1 N N N
5 G N N N 3 0 0
6 D N N N N N N
我希望能够对这些列进行求和,并且可以使用 SELECT SUM(A1+B1+C1+A2+B2+C2) FROM Results WHERE id ='1'< 手动执行此操作
我的问题是,每周我都会添加额外的 2 或 3 个游戏结果 - 因此我需要手动将新游戏添加到 SUM() 中。
我认为我可以使用类似 SUM(...) AFTER Name WHERE id = 'id'
我希望我可以在 Name 之后连接所有列的名称,然后将其添加到 SUM(连接列)中。
但我还没有找到一个很好的例子来工作。
最佳答案
如果您不关心“游戏”元数据,您可以有两个表,我认为不是,因为您已准备好将游戏列为列。
table:players (rows unique by id)
id name
1 A
2 B
table:scores (rows unique by combination of game_id and player_id)
game_id, player_id, score
A1 1 3
A1 2 1
B1 1 0
然后您可以连接这两个表来获得所有游戏中每位玩家的得分总和
select players.id, players.name, sum(scores.score) from players inner join score
on players.id = scores.player_id group by players.id, players.name
“分数”表中不会有任何未出现在游戏中的玩家的行,因此您不必处理任何空值。
关于mysql - mysql : Sum each row After Specific Row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41745516/