I have a table in which the id is constantly increasing. The Id
does not always increase by the same value and it is not an auto_increment
due to a api, but it increases permanently. Example:
Id | other content |
---|---|
501 | xxx |
500 | yyy |
365 | qqq |
… | … |
Now I just want to keep the last X rows, remove rows with lower ids. I was able to find a similar but not a working solution for myself to this problem.
I thought that something like that would work, but there is an error:
DELETE FROM `games` WHERE `matchId` NOT IN(SELECT `matchId` FROM `games` ORDER BY `matchId` DESC LIMIT X)
#1235 – This version of MySQL doesn’t yet support ‘LIMIT &
IN/ALL/ANY/SOME subquery’
Im running the mysql version 8.0.27 on a phpmyadmin 5.1.1 web interface.
Is there a way to keep the newest x rows without triggering this error?
2
Answers
You can use a CTE instead: