I'm using the code below to calculate the total score for my teams :
My tables are also listed bellow .
The problem is that the scores are being incorrectly calculated for every team except the first one . The image should explain the desired outcome based on the info in my database.
Query :
mysqli_query($connection, "UPDATE teams SET totalscore=overall_score+IFNULL((SELECT sum(overall_user_score) FROM users WHERE team_id=id),0)") or die(mysqli_error($connection));
Tables :
users(name, team_id, overall_user_score)
teams(id, name, team_score, totalscore)
Image:
I'm not sure why you have overall_score and totalscore as two different columns, but anyways:
If you are summing the totalscore column based on the overall_user_score column from the users table, you don't need to keep adding overall_score (in the teams table) to the totalscore column. See here as an example.
Again, I'm not sure why you have both overall_score and totalscore as two different columns. If I'm misunderstanding the question, let me know.
Really stupidly left out the t. infront of the last id so query should be :
mysqli_query($connection, "UPDATE teams t SET totalscore=overall_score+IFNULL((SELECT sum(overall_user_score) FROM users WHERE team_id=t.id),0)") or die(mysqli_error($connection));