0
    "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 和 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;
4

1 回答 1

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
于 2013-05-19T08:22:57.550 回答