I have a messages table with a client_id column (think of it as different chat channels) and I'd like to remove everything except the last N messages by client_id, meaning that I'd like to keep only the last N messages for each channel in the table.
Example data:
id,message,client_id,submitter
1,hello world,1,someUser
2,another message,3,johndoe
3,hello world,2,testUser
4,hello someUser,1,someoneElse
I could do something like the following, but that would delete everything except the last @limit rows.
DELETE FROM messages WHERE id NOT IN (SELECT id FROM messages ORDER BY id DESC LIMIT @limit)
Is there a way to do it in 1 query instead of selecting distinct client_id and then removing for each?