0

我一直在玩这个 SQL 代码:

SELECT
    id,
    @prev := @curr as prev,
    @curr := measure as curr,
    @rank := IF(@prev > @curr, @rank+@ties, @rank) AS rank,
    @ties := IF(@prev = @curr, @ties+1, 1) AS ties,
    (1-@rank/@total) as percentrank
FROM
    mytable,
    (SELECT
        @curr := null,
        @prev := null,
        @rank := 0,
        @ties := 1,
        @total := count(*) from mytable where measure is not null
    ) b
WHERE
    measure is not null
ORDER BY
    measure DESC

我想在名为“percentile”的列中将计算出的“percentrank”写回 mytable 的每个相应行,但我不记得如何在我的更新语句中工作。

我很感激帮助。

归功于SQL 的http://code.openark.org/blog/mysql/sql-ranking-without-self-join

4

1 回答 1

1

要从子查询更新,请给子查询一个别名,使其成为派生表。然后使用以下语法:

update YourTable
set SomeField = DerivedTable.something
, etc
from YourTable join
(subquery goes here) DerivedTable on YourTable.Whatever  = DerivedTable.Whatever
etc
于 2013-10-24T01:01:06.763 回答