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