muddy9494 Posted June 29, 2009 Share Posted June 29, 2009 Ok, well i have a chat script. And every 20 or so seconds it refreshes. When it does this, i also want it to removed all the rows except for the 15 latest ones. How would I do this? Link to comment https://forums.phpfreaks.com/topic/164120-only-keep-x-ammount-of-rows-in-a-table/ Share on other sites More sharing options...
Asheeown Posted June 29, 2009 Share Posted June 29, 2009 Where is it pulling the chat from? Link to comment https://forums.phpfreaks.com/topic/164120-only-keep-x-ammount-of-rows-in-a-table/#findComment-865752 Share on other sites More sharing options...
muddy9494 Posted June 29, 2009 Author Share Posted June 29, 2009 A database. I just want it to remove all but the 15 latest entries from the database. All the posts are sorted by id's too so yeah Link to comment https://forums.phpfreaks.com/topic/164120-only-keep-x-ammount-of-rows-in-a-table/#findComment-865759 Share on other sites More sharing options...
Asheeown Posted June 29, 2009 Share Posted June 29, 2009 SELECT * FROM Messages ORDER BY Id DESC LIMIT 15 Replace "Messages" with your messages table, add any 'WHERE' statements you may have before the order by command. The key is the LIMIT 15 at the end, tells it to only pull 15 records Link to comment https://forums.phpfreaks.com/topic/164120-only-keep-x-ammount-of-rows-in-a-table/#findComment-865764 Share on other sites More sharing options...
muddy9494 Posted June 29, 2009 Author Share Posted June 29, 2009 Yes i know that. That is how it is working now. But I want it to delete all of the ones not showing =p Link to comment https://forums.phpfreaks.com/topic/164120-only-keep-x-ammount-of-rows-in-a-table/#findComment-865770 Share on other sites More sharing options...
aggrav8d Posted June 29, 2009 Share Posted June 29, 2009 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. Link to comment https://forums.phpfreaks.com/topic/164120-only-keep-x-ammount-of-rows-in-a-table/#findComment-865778 Share on other sites More sharing options...
Asheeown Posted July 1, 2009 Share Posted July 1, 2009 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. Link to comment https://forums.phpfreaks.com/topic/164120-only-keep-x-ammount-of-rows-in-a-table/#findComment-867042 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.