0

任何人都知道我如何获得与每个游戏 ID 的最高分数相对应的日期,每个级别请:

----------------------------------------
|id |game_id |level |score |date       |
----------------------------------------
|1  |0       |1     |90    |1391989720 |
|1  |0       |1     |95    |1391989721 |
|1  |1       |1     |4     |1391989722 |
|1  |1       |1     |8     |1391989723 |
|1  |1       |2     |6     |1391989724 |
----------------------------------------

    SELECT cdu_user_progress.game_id, 
           cdu_user_progress.level, 
           max_score, 
           cdu_user_progress.date 
    FROM 
    (
        SELECT game_id, level, MAX(score) AS max_score
        FROM cdu_user_progress 
        GROUP BY game_id, level 
    ) AS ms 
    JOIN cdu_user_progress ON cdu_user_progress.game_id = ms.game_id AND 
                              cdu_user_progress.level = ms.level AND 
                              cdu_user_progress.score = ms.max_score 

现在,如果我还想获得 FIRST game_id(每个级别)的分数和日期,该怎么办,以获得此输出:

-----------------------------------------------------------------
|game_id |level |max_score |max_date   |first_score |first_date |
-----------------------------------------------------------------
|0       |1     |95        |1391989721 |90          |1391989720 |
|1       |1     |8         |1391989723 |4           |1391989722 | 
|1       |2     |6         |1391989724 |6           |1391989724 |
-----------------------------------------------------------------
4

1 回答 1

0

因为是mysql,所以你可以简单地这样做来拉取每场比赛得分最高的整行:

select * from (
  select *
  from cdu_user_progress
  order by score desc) x
group by game_id

这是因为 mysql 非标准 group by 功能,当不是所有非聚合列都在 group by 子句中命名时,它返回每个组遇到的第一行。

于 2014-02-10T14:24:54.803 回答