2011-05-18 57 views
0

如何獲得有史以來最好的排名,考慮最佳時間和得分?假設用戶贏得了幾次,那麼如何計算最佳用戶贏得次數以及平均獲勝時間是多少?如何使SQL查詢排名?

+----------------+----------------+--------+-----------------------+ 
| user_id  | quiz_id  | score | finish    | 
+----------------+----------------+--------+-----------------------+ 
| 1    | 1    | 1  | 2011-05-18 21:39:00 | 
| 2    | 1    | 1  | 2011-05-18 21:43:10 | 
| 3    | 1    | 0  | 2011-05-18 21:40:55 | 
| 1    | 2    | 1  | 2011-05-18 22:51:57 | 
| 2    | 2    | 1  | 2011-05-18 22:21:37 | 
| 3    | 2    | 0  | 2011-05-18 22:22:48 | 
| 4    | 2    | 1  | 2011-05-18 22:58:14 |    
+----------------+----------------+--------+-----------------------+ 
+0

開始時間在哪裏? – manji 2011-05-18 23:08:23

+0

在測驗桌上。 – luzny 2011-05-19 06:28:19

回答

1

爲了獲得最好的分數,考慮到最新的完成日期時間很容易。

總體

SELECT user_id, quiz_id, score, finish 
FROM table 
ORDER BY score DESC, finish DESC 
LIMIT 1 

對於每次測驗

SELECT user_id, quiz_id, score, finish 
FROM table 
GROUP BY quiz_id 
ORDER BY score DESC, finish DESC 

爲每個用戶

SELECT user_id, quiz_id, score, finish 
FROM table 
GROUP BY user_id 
ORDER BY score DESC, finish DESC 

我不知道你是什麼以用戶贏的數量來表示。什麼意味着勝利?與平均時間相同;這裏沒有足夠的信息來幫助你解決這兩個問題。