"id" "type" "parent" "country" "votes" "perCent"
"1" "1" "0" "US" "0" "0"
"2" "2" "1" "US" "0" "0"//votes = 8 i.e total of id3 votes and id7votes. Both have id=2 as parent, and id=2 is type 2
"3" "3" "2" "US" "4" "0"
"7" "3" "2" "US" "4" "0"
"19" "3" "1" "US" "4" "0"
"4183" "10" "3" "US" "2" "0"
"4184" "10" "3" "US" "2" "0"
"4185" "10" "7" "US" "2" "0"
"4186" "10" "7" "US" "2" "0"
"4187" "10" "19" "US" "2" "0"
"4188" "10" "19" "US" "2" "0"
我正在尝试type=2
用其 id 为父级的投票总和来更新 col。我一直在尝试以下方法,但似乎无处可去,因为这涉及 3 条语句,而且我在连接和多项选择方面非常落后。
UPDATE likesd a
INNER JOIN
(
SELECT parent, SUM(votes) totalVotes
FROM likesd
WHERE type = 3
GROUP BY parent
) b ON a.country = b.country
SET a.votes = b.totalVotes
WHERE a.id = b.parent;
它实际上是这样的:
select id from likesd where type = 2
select sum(votes) as totalVotes where parent = id
update likesd set votes = totalVotes where id = parent and country = country
知道如何做到这一点。我可以选择两次,但第三次被卡住了。
编辑: 类型 = 表中的 2 次重复