我正在尝试选择sql中的等级,选择查询工作得很好,如何将此选择转换为update并更新列。
SET @i=0 ;
SELECT sno, email, points, @i:=@i+1 AS rank FROM user ORDER BY points DESC
如何在表格中也更新此选择
从here
获得查询答案 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