2012-09-22 70 views
2

可能重複:
Mysql rank function如何從MySQL表計算排名

我在MySQL如下表:

=================================================================== 
id | member| total_correct_question | time_taken_in_seconds | 

=================================================================== 
1 | abc |  2     |  4    | 
2 | def |  3     |  6    | 
3 | ghi |  3     |  4    | 
4 | jkl |  3     |  5    | 
==================================================================== 

我要顯示的排名每個用戶從mysql查詢。任何人都可以向我提出這樣的疑問嗎?

+0

歡迎計算器。這是一個非常普遍的問題。請在發佈之前先花一分鐘搜索檔案。通常一個問題已經被問及已經回答 - 往往是幾次。 – Leigh

回答

1

試試這個:

SELECT @i:[email protected]+1 AS rank,a.* 
    FROM 
     (SELECT * 
     FROM Your_table 
     ORDER BY total_correct_question DESC, 
       time_taken_in_seconds)a,(SELECT @i:=0) r 


SQL Fiddle demo

+0

Wooow ...,其工作精細謝謝...約翰 – PHL

+0

@ user1690370:請接受最好的答案... http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-工作 –

1

您可以創建從原來的查詢生成笛卡爾的產品和最有趣的是,你可以通過一個增加它的子查詢( 然後得到它的排列號)。

SELECT @rankNo:[email protected]+1 RankNo, 
      a.* 
FROM  Table1 a, (SELECT @rankNo:=0) r 
ORDER BY `total_correct_question` DESC, 
     `time_taken_in_seconds` ASC 

SQLFiddle Demo

+0

Wooow ...,其工作很好謝謝...約翰 – PHL