"id" "type" "parent" "country" "votes" "perCent"
"1" "1" "0" "US" "100" "0"
"2" "1" "0" "US" "50" "0"
"3" "100" "0" "US" "150" "0" ->150 = sum(votes) where type = 1 and country = country
"4" "1" "0" "SE" "50" "0"
"5" "1" "0" "SE" "25" "0"
"6" "100" "0" "SE" "75" "0" ->75 = sum(votes) where type = 1 and country = country
我正在尝试使用各自国家/地区的所有type=100
的总计更新type=1
。
我一直在努力使用这个SQL,似乎无处可去。基本上,我要做的是更新其中type = 100的投票,其各自国家的类型= 1的总和。 我一直试图调整这个,但似乎完全失败了。你能帮忙吗?
UPDATE likes p
JOIN likes h
ON p.country = h.country
AND (p.type=1) AND h.type=1
SET p.votes=sum(h.votes) where p.type=100;
答案 0 :(得分:2)
UPDATE tableName a
INNER JOIN
(
SELECT country, SUM(votes) totalVotes
FROM tableName
WHERE type = 1
GROUP BY country
) b ON a.country = b.country
SET a.votes = b.totalVotes
WHERE a.type = 100