mysql:限制结果集中每个播放器返回的记录数

dxpyg8gm  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(532)

如何更改以下查询以限制结果,使每个人只显示两个游戏而不是四个?

这将使我更容易回答这个问题,这两个人最近都玩了哪两个游戏?或者类似的大集合问题,其中每个人返回许多记录。
我在mysql中通过phpmyadmin使用下面的sql来返回一个游戏列表,供一组回答问题的人使用,对于这组人,他们最近玩了哪些游戏?
目前每个人玩过的不同游戏都会出现在结果中。我想知道我是否可以限制每个人的游戏数量,例如两个。这样我就可以更容易地回答上面的问题了。
playgroup.fullname是一个包含我要为其创建列表的四个人的表。

查询

  1. SELECT Games.Title, playgroup.FullName, Max(Responses.PlayDate)
  2. FROM (Players INNER JOIN (Games INNER JOIN Responses ON Games.ID = Responses.Title) ON Players.ID = Responses.Player) INNER JOIN PlayGroup ON Players.ID = PlayGroup.FullName
  3. GROUP BY Games.Title, PlayGroup.FullName
  4. ORDER BY playgroup.FullName ASC, Max(Responses.PlayDate) DESC;

当前结果

对查询的什么更改会导致每个全名只返回两个游戏?

  1. Title,
  2. FullName,
  3. Max(Responses.PlayDate)
  4. Dutch Blitz
  5. 1
  6. 2020-06-14 00:00:00.000000
  7. PitchCar
  8. 1
  9. 2020-06-13 00:00:00.000000
  10. Bohnanza
  11. 1
  12. 2020-05-31 00:00:00.000000
  13. Geocaching
  14. 1
  15. 2020-05-25 00:00:00.000000
  16. Patchwork
  17. 2
  18. 2020-05-26 00:00:00.000000
  19. Dominion
  20. 2
  21. 2020-05-25 00:00:00.000000
  22. Geocaching
  23. 2
  24. 2020-05-25 00:00:00.000000
  25. Disc Golf
  26. 2
  27. 2020-05-24 00:00:00.000000
  28. Dutch Blitz
  29. 4
  30. 2020-06-14 00:00:00.000000
  31. Bohnanza
  32. 4
  33. 2020-05-31 00:00:00.000000
  34. Ingenious
  35. 4
  36. 2020-04-28 00:00:00.000000
  37. Qwirkle
  38. 4
  39. 2020-04-08 00:00:00.000000
  40. Colossal Arena
  41. 141
  42. 2020-06-19 00:00:00.000000
  43. Roll for the Galaxy
  44. 141
  45. 2020-06-18 00:00:00.000000
  46. Fortnite
  47. 141
  48. 2020-06-16 00:00:00.000000
  49. Pendragon
  50. 141
  51. 2020-06-15 00:00:00.000000
6l7fqoea

6l7fqoea1#

可以使用窗口函数:

  1. SELECT title, fullname, max_playdate
  2. FROM (SELECT g.Title, pg.FullName, Max(r.PlayDate) as max_playdate,
  3. ROW_NUMBER() OVER (PARTITION BY g.Title ORDER BY MAX(r.PlayDate) DESC) as seqnum
  4. FROM Players p INNER JOIN
  5. Games g INNER JOIN
  6. Responses r
  7. ON r.ID = r.Title JOIN
  8. Players p
  9. ON p.ID = r.Player INNER JOIN
  10. PlayGroup pg
  11. ON p.ID = pg.FullName
  12. GROUP BY g.Title, pg.FullName
  13. ) g
  14. ORDER BY FullName ASC, max_PlayDate DESC;

相关问题