假设有test表,下图为表机构和数据,score表示积分。现在要查询积分排名为第几的id??

查询语句
select id,score,(@rowno:=@rowno+1) as rowno from test,(select (@rowno:=0)) b order by score desc;
查询结果:

这样就得到了根据积分排名的位置了(rowno),要获得id只要在这个表的基础上进行查询就可以了
select id from (select id,score,(@rowno:=@rowno+1) as rowno from test,(select (@rowno:=0)) b order by score desc) c where rowno=1;rowno的值为你想要查询的名次,就得到对应的id了。
上面的rowno为1,排名第一的id就是2了

本文详细介绍了如何使用SQL语句查询数据库中积分排名为第几的ID,通过设置变量并利用排序功能,实现快速定位指定排名的用户ID。
1127

被折叠的 条评论
为什么被折叠?



