Question

I have a table with players, and a table with profiles. Profiles(users) can "claim" players, so that the "profile" field in a player record gets set to the id of the profile.

I want a totalscore property(which contains the sum of all previous score values) in my profiles table. To calculate data commited in the past, i've written the following query:

UPDATE profiles,players
SET profiles.`totalscore` = profiles.`totalscore` + players.`score`
WHERE players.`profile`=profiles.`id`

However, totalscore gets set to the last found value. How would i solve this?

Était-ce utile?

La solution

You have to use group by.

UPDATE profiles
inner join (
select profileid,SUM(score) as playersscore
from players
group by
profileid) players
ON players.profileid=profiles.id
SET profiles.totalscore = profiles.totalscore + playersscore
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top