What is the right way to write an sql that'll update all type = 1
with the sum of totals of rows where parent = id of row with type=1
.
Put simply: update likesd set totals = sum of all totals where parent = id of row where type = 1
"id" "type" "parent" "country" "totals"
"3" "1" "1" "US" "6"
"4" "2" "3" "US" "6"
"5" "3" "3" "US" "5"
Desired results
"id" "type" "parent" "country" "totals"
"3" "1" "1" "US" "17" ->6+6+5=17
"4" "2" "3" "US" "6"
"5" "3" "3" "US" "5"
I was trying with (and failed)
UPDATE likesd a
INNER JOIN (
SELECT parent, sum(totals) totalsNew
FROM likesd
WHERE b.parent = a.id
GROUP BY parent
) b ON a.id = b.parent
SET a.totals = b.totalsNew;