MySQL:保留每个用户最近的 10 行

Max*_*hel 5 mysql delete

我有一个包含 3 列的游戏保存分数的表格(在实践中更多,但这种方式对每个人来说都更容易):

userid   : the id of the user that got the score
timestamp: the time the score was gotten
score    : the score itself
Run Code Online (Sandbox Code Playgroud)

查询的目标是删除行,因此每个用户最多有 10 个分数。我们还希望保留最近的行。所以我们需要删除旧的,直到我们得到 10。注意有些玩家的分数可能少于 10。

我将如何设法做到这一点?

小智 0

按时间戳对行进行排名,并删除任何排名大于 10 的行。