2

当行中的某个值相同时,添加整数列的有效方法是什么?

例如,假设我有一张桌子,上面有两个不同玩家的分数。

id | score | player_id
1      5        1
2      6        1
3      9        2
4      3        2

如何根据 id 添加玩家的分数?我不确定这个选择的最后一部分:

SELECT sum(scores.score) FROM scores WHERE player_id = player_id;
4

3 回答 3

2

我想你想要GROUP BY

SELECT player_id, SUM(score) totalScore
FROM tablename
GROUP BY player_id

输出

╔═══════════╦════════════╗
║ PLAYER_ID ║ TOTALSCORE ║
╠═══════════╬════════════╣
║         1 ║         11 ║
║         2 ║         12 ║
╚═══════════╩════════════╝
于 2013-09-18T20:30:52.620 回答
1

你可以这样尝试:-

SELECT player_id, SUM(score) as Sums
FROM tablename
GROUP BY player_id
于 2013-09-18T20:31:14.030 回答
1

使用 GROUP BY

SELECT player_id, sum(scores) AS Total FROM scores GROUP BY player_id
于 2013-09-18T20:31:20.827 回答