[mysql]rank的作法
發表於 : 2012年 12月 10日, 18:22
在 mysql 中沒有 rank function 可以進行排行. 不過有變通的做法, 利用一個排行變數進行排行, 如下:
就可以排出依 grade 的排行名次了.
其他相關資料:
http://ithelp.ithome.com.tw/question/10069933
代碼: 選擇全部
create table test (sid int, grade int);
insert into test values (1, 15);
insert into test values (2, 50);
insert into test values (3, 26);
insert into test values (4, 80);
select sid, @rank := @rank + 1 from test a, (select @rank := 0) b order by grade desc
其他相關資料:
http://ithelp.ithome.com.tw/question/10069933