I have a table in which I need to keep the total number of rows within 50,000. This table includes an Id field (id is auto incremental).
How can I check if max(id) - min(id) > 50,000, then delete the earlier rows?
DELETE FROM news WHERE if (max(id) - min(id) > 50000)
This query will delete all the rows if max(id) - min(id) > 50000, what is the correct way? Ideally I need a one line command, excuse in SSH method. thanks.
The above will not leave exactly 50K rows of course, as there may be gaps in the
idsequence. But I guess this is expected and not a problem. If you really want to leave exactly 50K rows, any statement will probably be less efficient. You can try this one: