当行中的某个值相同时,添加整数列的有效方法是什么?
例如,假设我有两张不同球员的桌子。
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;
答案 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