mysql 测验排行榜按分数、所用时间过滤

mysql Quiz leaderboard filter by points, time taken

我有一个测验报告 table,其中显示了用户参加的每个测验的报告。我需要从中创建一个排行榜,它显示最高用户的最佳分数,按 过滤,然后 用时 过滤。

这里是一个link到一个sqlfiddlehttp://sqlfiddle.com/#!2/65fbf0/1

我真的很挣扎,因为我需要为一个用户按两列过滤结果,我的理想结果是

Results for Quiz id 1
---------------------------------------------------------------   
| user_id  |  points  |  time_spend  |  start_dt  |  quiz_id  |
| 1        |  3       | 0.5          | May,15 2015|  1        |
| 2        |  3       | 0.8          | May,15 2015|  1        |
| 3        |  2       | 0.5          | May,15 2015|  1        |

然后对显示上周结果的所有测验进行单独查询

Results from all Quizzs
---------------------------------------------------------------   
| user_id  |  points  |  time_spend  |  start_dt  |  quiz_id  |
| 1        |  3       | 0.5          | May,15 2015|  1        |
| 2        |  3       | 0.8          | May,13 2015|  3        |
| 3        |  2       | 0.5          | May,12 2015|  2        |

您可以像这样对多列进行排序:

select *
from QuizReport
where quiz_id = 1
order by points desc, time_spend asc;

select *
from (
  select *
  from QuizReport
  where start_dt >= subdate(curdate(), 7)
  order by points desc, time_spend asc) a
group by user_id;

group_by user_id 为每个 user_id 保留第一行。由于内部查询按分数对行进行排序,因此外部查询将为每个用户显示最佳行。