SQL - 为每个玩家的最后10个结果中的每个玩家选择最佳结果

时间:2018-03-01 11:10:25

标签: mysql sql

我需要从我的桌面游戏中创建一个SQL请求:

user_id   playDate      score   game_id
1   27/02/2018 16:21    77244   119
1   27/02/2018 16:20    218364  119
1   27/02/2018 16:08    272240  119      <--- This one
1   27/02/2018 16:08    118055  119
1   27/02/2018 16:08    173677  119
1   27/02/2018 16:08    112328  119
1   27/02/2018 16:08    169296  119
1   27/02/2018 16:08    56090   119
1   27/02/2018 16:08    51172   119
1   27/02/2018 16:08    95293   119
1   27/02/2018 16:08    109572  119
1   27/02/2018 16:07    20421   119
1   27/02/2018 16:07    458186  119      <--- NOT This one because not in 10 last play
1   27/02/2018 16:07    196987  119
1   27/02/2018 16:07    75904   119
1   27/02/2018 16:07    117530  119
2   01/03/2018 10:54    223446  119
2   01/03/2018 10:52    617292  119
2   01/03/2018 10:51    177332  119
2   01/03/2018 10:48    41071   119
2   01/03/2018 10:47    103833  119
2   01/03/2018 10:46    327581  119
2   01/03/2018 10:41    737348  119
2   01/03/2018 10:36    8476    119
2   01/03/2018 10:36    252481  119
2   28/02/2018 18:21    205360  119
2   28/02/2018 18:19    63601   119
2   28/02/2018 18:19    242404  119
2   28/02/2018 18:13    18503   119
2   28/02/2018 18:03    167226  119
2   06/01/2018 14:21    386115  119      <--- This one
2   06/01/2018 11:25    216266  119
2   06/01/2018 11:25    149841  119
5   28/02/2018 16:13    402902  119      <--- This one
5   28/02/2018 12:11    156313  119
5   28/02/2018 11:45    192914  119
5   28/02/2018 11:44    117458  119

我的目标是获得:

每个user_id的MAX(得分) 仅查看user_id最后10次播放(通过Order BY playDate DESC) 仅限1场比赛_Id

这些数据的预期结果如下:

user_id Best score
  5      402902
  2      386115
  1      272240

我尝试过GROUP_CONCAT,LIMIT等但找不到合适的解决方案.. 非常感谢 ! 奥利维亚

3 个答案:

答案 0 :(得分:0)

请尝试以下操作。

SELECT user_id, max(score) as maxscore
FROM table_name
WHERE game_id = 119
GROUP BY user_id
ORDER BY playDate DESC LIMIT = 10;

答案 1 :(得分:0)

我认为我找到了一个解决方案,但任何建议/意见都将不胜感激!

SELECT
  play.user_id AS id,
  fos_user.username AS username,
  MAX(play.score) AS score
FROM
  play INNER JOIN (
    SELECT
      user_id,
      GROUP_CONCAT(score ORDER BY playDate DESC) grouped_score
    FROM
      play
    WHERE game_id = :game
    GROUP BY user_id) group_max
  ON play.user_id = group_max.user_id
     AND FIND_IN_SET(score, grouped_score) BETWEEN 1 AND :limit
JOIN fos_user ON play.user_id = fos_user.id 
GROUP BY 
    play.user_id
ORDER BY
    play.score DESC

答案 2 :(得分:0)

试试这个

select user_id, max(score) from play where game_id = 119 GROUP BY user_id order by playDate desc limit 10;