Skip to content

Instantly share code, notes, and snippets.

@cerkauskas
Last active September 24, 2015 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cerkauskas/690d388771a1bd496b39 to your computer and use it in GitHub Desktop.
Save cerkauskas/690d388771a1bd496b39 to your computer and use it in GitHub Desktop.
-- this select gets correct data
SELECT `rez_clubs`.`id`, `rez_clubs`.`name`, SUM(`rez_participants`.`points_1`)
FROM `rez_clubs`
JOIN `rez_participants` ON `rez_participants`.`club_id` = `rez_clubs`.`id`
GROUP BY `rez_participants`.`club_id`
-- what im trying to achieve
UPDATE `rez_clubs`
JOIN `rez_participants`
SET `rez_clubs`.`points_1` = SUM(`rez_participants`.`points_1`)
WHERE `rez_clubs`.`id` = `rez_participants`.`club_id`
GROUP BY `rez_clubs`.`id`
@Naktibalda
Copy link

UPDATE `rez_clubs`
JOIN (
  SELECT `club_id`, SUM(`rez_participants`.`points_1`) AS points_sum
  FROM  `rez_participants` 
  GROUP BY `club_id`
) AS club_points
ON club_id = rez_clubs.id
SET rez_clubs.points_1 = club_points.points_sum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment