mysql - 根据表值添加整数的有效方法

标签 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;

最佳答案

我想你想要GROUP BY

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

输出

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

关于mysql - 根据表值添加整数的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18881764/

相关文章:

mysql - 在查询中包含可重复使用的文本 block

c# - 如何在c#中的sql表中的单词后插入换行符

mysql - 来自单行的 SQL 联合

sql - Postgresql 自定义排序

php - 插入时数组到字符串的转换

php - mysql:未知选项 '--includedir/etc/mysql/mariadb.conf.d/'

mysql - 使用 SQL 导出时清理值(如果为 null 或类似值)

sql - mysql 多重查询

php - 列出时显示重音

mysql - eclipselink 和 mysql 的 GTID 复制