mysql 排序后获得某行的位置

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

查询语句

[sql] view plain copy
 
  1. select id,score,(@rowno:=@rowno+1) as rowno from test,(select (@rowno:=0)) b order by score desc;  

查询结果:

这样就得到了根据积分排名的位置了(rowno),要获得id只要在这个表的基础上进行查询就可以了

 

[sql] view plain copy
 
  1. 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了

 

全文:http://blog.csdn.net/sugang_ximi/article/details/6703804 //搜索 mysql 查询记录在数据表第几行 set as 学习更多

MySQL查询结果集中得到记录行号的实现

http://database.51cto.com/art/201011/235158.htm

posted @ 2016-05-20 18:16  stma  阅读(353)  评论(0)    收藏  举报