【发布时间】:2013-11-18 14:12:21
【问题描述】:
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 进行更新,一切正常。但是当我添加更新时,我得到了这个错误。
最终结果是使用来自选择的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"
【问题讨论】:
-
sqlfiddle 是否适合其他人?
-
@Mihai 是的,它已经关闭了。