转换选择查询以更新查询

时间:2019-01-07 09:14:30

标签: mysql sql

我正在尝试选择sql中的等级,选择查询工作得很好,如何将此选择转换为update并更新列。

SET @i=0  ;

SELECT sno, email, points, @i:=@i+1 AS rank FROM user ORDER BY points DESC

如何在表格中也更新此选择

here

获得查询

2 个答案:

答案 0 :(得分:1)

SET @i=0; 
UPDATE user SET rank= @i:= (@i+1) ORDER BY points DESC;

答案 1 :(得分:0)

假设sno是表的主键,您需要:

SET @i=0  ;
UPDATE user
JOIN (
    SELECT sno, email, points, @i:=@i+1 AS rank
    FROM user 
    ORDER BY points DESC
) AS ranks ON ranks.sno = user.sno 
SET user.rank = ranks.rank