UPDATE likesd a
INNER JOIN (
select id, perCent, rank
from
(
select id, perCent, elmTotals
, @curRank :=
case
when @prevParent = parent then
case
when @prevCent = perCent then @curRank
else @curRank + 1
end
else 1
end rank
, @prevParent := parent
, @prevCent := perCent
from
(
select child.id, child.perCent, child.parent
from likesd parent
inner join likesd child
on child.parent = parent.id and child.country = parent.country
where parent.type = 3
order by parent.id, child.perCent desc
) b
cross join (SELECT @curRank := 0, @prevParent := null, @prevCent := null) c
) d ) ON a.country = d.country and a.id = d.id
SET a.rank = d.rank
WHERE a.type = 10;
在上面的sql中,即使我的所有派生表都有别名,我也会收到错误:/* SQL Error (1248): Every derived table must have its own alias */
。一切正常,没有包围主sql的更新。但是当我添加更新时,我得到了这个错误。
最终结果是使用select中的rank
更新列rank
。这只是给出问题的更新。里面很大的选择很好。
下面是下表,这是一个带有一些数据的fiddle
主要表
"id" "type" "parent" "country" "votes" "perCent" "rank"
"24" "1" "1" "US" "35" "0"
"25" "3" "24" "US" "35" "0"
"26" "10" "25" "US" "15" "50.00"
"27" "10" "25" "US" "10" "33.33"
"28" "10" "25" "US" "10" "33.33"
"29" "1" "1" "US" "50" "0"
"30" "3" "29" "US" "50" "0"
"31" "10" "30" "US" "20" "40.00"
"32" "10" "30" "US" "15" "25.00"
"33" "10" "30" "US" "15" "35.00"
答案 0 :(得分:2)
UPDATE likesd a
INNER JOIN (
select id, perCent, rank
from
(
select id, perCent, elmTotals
, @curRank :=
case
when @prevParent = parent then
case
when @prevCent = perCent then @curRank
else @curRank + 1
end
else 1
end rank
, @prevParent := parent
, @prevCent := perCent
from
(
select child.id, child.perCent, child.parent
from likesd parent
inner join likesd child
on child.parent = parent.id and child.country = parent.country
where parent.type = 3
order by parent.id, child.perCent desc
) b
cross join (SELECT @curRank := 0, @prevParent := null, @prevCent := null) c
) d )f ON a.country = d.country and a.id = d.id
SET a.rank = d.rank
WHERE a.type = 10;
答案 1 :(得分:2)
您缺少一个别名:
cross join (SELECT @curRank := 0, @prevParent := null, @prevCent := null) c
) d ) ON a.country = d.country and a.id = d.id
^ Here