I have a mysql table that I need to only contain 20 newest records before adding additional records.
New rows are added daily so I would like it first delete any records that are greater than the 20 allowed starting with the earliest.
The table contains an auto increment “id” column so I can easily determine which is the earliest records.
Thanks for any help.
You can specify an offset with the
LIMITkeyword in your query so the newest 20 rows are kept. According to MySQL’s documentation, however, there’s no easy way to limit from an offset all the way to the last; instead, they suggest this:So this SQL should do the trick: