0

我有一个名为“排名”的表格,它基本上保存了按“点”排序的用户排名位置。它的:

*Id - 整数 - AI(这是位置。1 = 第一个。最高分)
*IdUser - 整数
*PointsNow - 整数
*PointsBefore - 整数
*PositionBefore - 整数

我想要实现的是有一些方法可以知道用户是否进入了最高级别的位置,或者他现在是否更低。

例如,这些是一些条目:

1 | 第2236章 41 | 0 | 0

2 | 551 | 40 | 0 | 0

3 | 第1557章 34 | 0 | 0

所以我认为我可以做到这一点:

*删除所有条目

*将自动增量重置为1

*将 PointsNow 复制到 PointsBefore 并将 Id 复制到 PositionBefore。

我不确定我是否应该这样做,或者是否有更合乎逻辑的选择。我会很感激一些帮助。

谢谢

4

1 回答 1

1

试试这个:

select @rownum:=@rownum+1 `rank`, u.idUser 
from User u, (SELECT @rownum:=0) r 
order by PointsNow desc;

如果您只想根据排名对用户进行排序:

SELECT * FROM User
ORDER BY PointsNow DESC;

样本数据:

CREATE TABLE User(
IdUser Integer primary key,
PointsNow Integer,
PointsBefore Integer,
PositionBefore  Integer);

INSERT INTO User(IdUser,PointsNow,PointsBefore,PositionBefore)
VALUES (2236,41,0,0),(551,40,0,0),(1557,34,0,0);

SQL 小提琴演示

需要注意的是,我省略了 id 列。使用触发器自动执行查询。有关排名计算的更多信息,请参阅。MySQL 更新语句来存储排名位置

于 2013-01-28T13:41:13.890 回答