MySQL:为quiz_id = something的每个用户获取最新的SCORE

时间:2014-06-06 12:53:04

标签: mysql distinct

所以这是我的表:

id     |     quiz_id     |     user_id     |     date_replied     |     score

1             1                  1           2014-06-04 10:11:47         50
2             1                  1           2014-06-04 20:52:53         90
3             1                  5           2014-06-05 14:22:59         80
4             1                  5           2014-06-06 18:23:41         70

我想要的是为scoreuser_id的每个quiz_id获取最新的quiz_id

因此1 = id | quiz_id | user_id | date_replied | score 2 1 1 2014-06-04 20:52:53 90 4 1 5 2014-06-06 18:23:41 70 ;它会回来:

{{1}}

有没有办法在MySQL中执行此操作?

谢谢!

1 个答案:

答案 0 :(得分:2)

select t1.* 
from your_table t1
join
(
  select user_id, max(date_replied) as mdate
  from your_table
  where quiz_id = 1
  group by user_id
) t2 on t1.user_id = t2.user_id and t1.date_replied = t2.mdate