简体   繁体   English

使用ORDER BY DESC排名,但更新该列

[英]Ranking with ORDER BY DESC, but updates the column

Hey I have a ladder ranking with this data: 嘿,我对此数据有一个阶梯排名:

http://i.stack.imgur.com/eNCOO.png

Ignore the wins, loses, lvl. 忽略胜利,失败,lvl。

The problem is, on the team table I have a column for the ranking, the ranking I'm using on the table on the image is just ai variable that starts in 0 and increments 1 when finishes the while cyclo. 问题是,在团队表上我有一列用于排名,我在图像上的表上使用的排名只是ai变量,该变量以0开始,并在完成while循环时递增1。 How do I update the ranking column with the real ranking ordered by Points? 如何用积分排序的真实排名更新排名列?

You can do this with an update / join : 您可以通过update / join

update rankings r join
       (select r.id, @rn := @rn + 1 as ranking
        from rankings r cross join
             (select @rn := 0) vars
        order by points desc
       ) rp
       on r.id = rp.id
    set r.ranking = rp.ranking;

The subquery uses variables to calculating the ranking. 子查询使用变量来计算排名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM