表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排序