我正在尝试将一个表的总和与另一个表的总和进行一次简单的更新,但是由于某种原因,它仅更新了一行。以下是表格中的相关信息:
游戏
gameplayer|points
----------------
John |5
Jim |3
John |3
Jim |4
球员职业
playercareername|playercareerpoints
-----------------------------------
John |0
Jim |0
现在最终,我希望运行更新后的最后一张表看起来像这样:
球员职业
playercareername|playercareerpoints
-----------------------------------
John |8
Jim |7
这是我尝试的查询,仅更新第一行:
UPDATE playercareer
SET playercareer.playercareerpoints =
(
SELECT
SUM(games.points)
FROM games
WHERE
playercareer.playercareername=games.gameplayer
)
我似乎找不到答案。在此先感谢您的宝贵时间和建议!
最佳答案
UPDATE playercareer c
INNER JOIN (
SELECT gameplayer, SUM(points) as total
FROM games
GROUP BY gameplayer
) x ON c.playercareername = x.gameplayer
SET c.playercareerpoints = x.total