我有一张这样的表:
PLAYER1_ID # PLAYER2_ID # SCORE_P1 # SCORE_P2
1 3 3 5
4 2 5 4
2 1 3 4
PDO中的查询是什么?原则是我不知道ID的数量......谢谢! :
PLAYER ID # SCORE
1 7
2 7
3 5
4 5
答案 0 :(得分:3)
更新:同样没有特定的PDO。你可以这样做
SELECT player_id, SUM(score) score
FROM
(
SELECT player1_id player_id, score_p1 score
FROM table_name
UNION ALL
SELECT player2_id, score_p2
FROM table_name
) q
GROUP BY player_id
输出:
| PLAYER_ID | SCORE | |-----------|-------| | 1 | 7 | | 2 | 7 | | 3 | 5 | | 4 | 5 |
这是 SQLFiddle 演示
对原始问题的回答:此处没有特定的PDO。只需使用GROUP BY
和汇总SUM()
函数
SELECT player_id, SUM(score) score
FROM table_name
GROUP BY player_id
输出:
| PLAYER_ID | SCORE | |-----------|-------| | 1 | 8 | | 3 | 6 | | 5 | 3 |
这是 SQLFiddle 演示