假設有test表,下圖爲表機構和數據,score表示積分。現在要查詢積分排名爲第幾的id??
查詢語句
- select id,score,(@rowno:=@rowno+1) as rowno from test,(select (@rowno:=0)) b order by score desc;
查詢結果:
這樣就得到了根據積分排名的位置了(rowno),要獲得id只要在這個表的基礎上進行查詢就可以了
- 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爲1,排名第一的id就是2了