數據庫表以下圖1,如今要按照分數對數據進行排序。java
圖1 圖2 圖3數據庫
方法一code
按照分數排序,相同分數序號相同排序
select a.*,(select count(*) from scores b where a.score < b.score)+1 as rank from scores a order by score desc
運行結果如圖2.class
方法二select
按照分數排序,序號依次增長方法
select a.*,@rownum:=@rownum+1 as rank from (select * from scores order by score desc) a, (SELECT @rownum:=0) r
運行結果如圖3.im