Refresh table using SUM from another table

I'm trying to do what I thought would be a simple table update with a sum from another table, but for some reason it only updates one row. Here is the relevant information from the tables:

games

gameplayer|points ---------------- John |5 Jim |3 John |3 Jim |4 

playercareer

 playercareername|playercareerpoints ----------------------------------- John |0 Jim |0 

Now, ultimately, I would like the last table to look like this after starting the update:

playercareer

 playercareername|playercareerpoints ----------------------------------- John |8 Jim |7 

This query, I tried to update only the first line:

 UPDATE playercareer SET playercareer.playercareerpoints = ( SELECT SUM(games.points) FROM games WHERE playercareer.playercareername=games.gameplayer ) 

It seems I can not find the answer to this question. Thank you in advance for your time and advice!

+6
source share
1 answer
 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 
+16
source

Source: https://habr.com/ru/post/912862/


All Articles