编写一个 sql 的正确方法是什么,它将type = 1
用 parent = id of row with 的行的总和来更新所有内容type=1
。
简单地说:更新 likesd set 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"
期望的结果
"id" "type" "parent" "country" "totals"
"3" "1" "1" "US" "17" ->6+6+5=17
"4" "2" "3" "US" "6"
"5" "3" "3" "US" "5"
我正在尝试(但失败了)
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;