基于表值添加整数的有效方法

时间:2013-09-18 20:28:35

标签: mysql sql

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

例如,假设我有两张不同球员的桌子。

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;

3 个答案:

答案 0 :(得分:2)

我想你想要GROUP BY

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

输出

╔═══════════╦════════════╗
║ PLAYER_ID ║ TOTALSCORE ║
╠═══════════╬════════════╣
║         1 ║         11 ║
║         2 ║         12 ║
╚═══════════╩════════════╝

答案 1 :(得分:1)

您可以尝试这样: -

SELECT player_id, SUM(score) as Sums
FROM tablename
GROUP BY player_id

答案 2 :(得分:1)

使用GROUP BY

SELECT player_id, sum(scores) AS Total FROM scores GROUP BY player_id