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


表test中现有数据

id score
1 10
2 5
3 15

 

 

 

 

执行sql

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

 

获得如下结果

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

 

 

 

 

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

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的记录的排序为2

 

 

 

 

 

 

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM