"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=1
总数来更新 type=100
。
我一直在努力处理这个sql,但似乎毫无进展。基本上,我想做的是更新各自国家/地区 type=100 的选票,其总和为 type=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;
最佳答案
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
关于mySql 使用行的 sum() 进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16632691/