MySQL GROUP BY两列

时间:2013-07-30 09:11:41

标签: mysql

我尝试首先获得用户每个级别的最高分,然后将每个分数相加以找到用户的整体高分。

+-----------+----------+----------+--------------------+
| id        | name     | score    | level_id           |
+-----------+----------+----------+--------------------+
|         1 | Jane Doe | 100      |                  1 | 
|         2 | Jane Doe | 50       |                  2 | 
|         1 | John Doe | 100      |                  1 |
|         2 | John Doe | 50       |                  2 |
+-----------+----------+----------+--------------------+

我已经设法找到用户在所有级别上的最高分,但我怎样才能为每个级别执行此操作

select user_id, id, max(correct_answers)
from score
group by user_id

3 个答案:

答案 0 :(得分:1)

试试这个

   select user_id, max(correct_answers) maxs ,level_id
   from score
   group  by level_id,user_id

DEMO

答案 1 :(得分:0)

按组添加level_id

select user_id, id, max(correct_answers)
from score group by user_id,level_id

答案 2 :(得分:0)

试试这个:

选择姓名,身份证,最高(分数) 从得分 group by level_id,id