mysql 排序后获取某一行的位置

表test中现有数据sql

id score
1 10
2 5
3 15

 

 

 

 

执行sqlspa

select id,score,(@rowno:=@rowno+1) as rowno from test,(select (@rowno:=0)) b order by score desc; 

 

得到以下结果.net

id score rowno
3 15 1
1 10 2
2 5 3

 

 

 

 

而后在此基础上查询某条记录的位置code

select rowno from (select id,score,(@rowno:=@rowno+1) as rowno from test,(select (@rowno:=0)) b order by score desc) c where id = 1;

这样就能够查到id为1的记录的排序为2blog

 

 

 

 

 

 

参考: http://blog.csdn.net/sugang_ximi/article/details/6703804排序

相关文章
相关标签/搜索