Jump to content

Only keep x ammount of rows in a table


muddy9494

Recommended Posts

So you never have more than 15 records in the table?  What if you start the table with 15 blank rows and then you

 

UPDATE `Messages` SET text='$whatever', sent=NOW() WHERE id=(SELECT id FROM `Messages` ORDER BY sent ASC LIMIT 1);

 

that way the system would just keep removing the oldest and replacing it with the newest.

When you need to display the 15 in order use

 

SELECT * FROM `Messages` ORDER BY sent ASC;

 

and you know you'll only get 15.  If you need to make the buffer longer add some rows and if you need to make it shorter remove a few rows.  This also avoids the problem of a really busy chat system overflowing your index count.

If you're still looking for an answer, you can use a NOT IN statement in a delete where clause.  Like so...

 

DELETE * FROM Messages WHERE Id NOT IN(SELECT * FROM Messages ORDER BY Id DESC LIMIT 15);

 

Now, I don't know if yours is DESC or ASC, could never distinguish without actually coding it and trying both.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.