MDanz Posted September 25, 2009 Share Posted September 25, 2009 how do i limit 20 records in a mysql table? so say i add 20 records, then i add another, it deletes the oldest record.. how do i accomplish this? Quote Link to comment https://forums.phpfreaks.com/topic/175492-limit-20-records-in-mysql-table/ Share on other sites More sharing options...
ILMV Posted September 25, 2009 Share Posted September 25, 2009 Can't you just let them all stack up (say 500 records) and when it comes to querying the database use the LIMIT in SQL to achieve your solution: SELECT * FROM table LIMIT 20 ILMV Quote Link to comment https://forums.phpfreaks.com/topic/175492-limit-20-records-in-mysql-table/#findComment-924732 Share on other sites More sharing options...
MDanz Posted September 25, 2009 Author Share Posted September 25, 2009 that doesn't delete records from mysql.. that only limits 20 to being displayed.. I want only 20 in mysql at all times... Quote Link to comment https://forums.phpfreaks.com/topic/175492-limit-20-records-in-mysql-table/#findComment-924735 Share on other sites More sharing options...
MadTechie Posted September 25, 2009 Share Posted September 25, 2009 assuming you have less than 100020 records, this will work, DELETE FROM `table` LIMIT 20 , 100000 Quote Link to comment https://forums.phpfreaks.com/topic/175492-limit-20-records-in-mysql-table/#findComment-924740 Share on other sites More sharing options...
ILMV Posted September 25, 2009 Share Posted September 25, 2009 Would you want to enforce an order by as well MacTechie? Quote Link to comment https://forums.phpfreaks.com/topic/175492-limit-20-records-in-mysql-table/#findComment-924864 Share on other sites More sharing options...
MadTechie Posted September 25, 2009 Share Posted September 25, 2009 True, order by timestamp would be idea, but autonumber maybe fine So add ORDER BY `timestamp` DESC ORDER BY `ID` DESC before the limit Quote Link to comment https://forums.phpfreaks.com/topic/175492-limit-20-records-in-mysql-table/#findComment-924871 Share on other sites More sharing options...
robert_gsfame Posted September 26, 2009 Share Posted September 26, 2009 or just do it with OFFSET which will tell the number of records Quote Link to comment https://forums.phpfreaks.com/topic/175492-limit-20-records-in-mysql-table/#findComment-925209 Share on other sites More sharing options...
MadTechie Posted September 26, 2009 Share Posted September 26, 2009 or just do it with OFFSET which will tell the number of records I am using an Offset! When you pass LIMIT two arguments the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return, using LIMIT row_count OFFSET offset is purely for compatibility with PostgreSQL. Quote Link to comment https://forums.phpfreaks.com/topic/175492-limit-20-records-in-mysql-table/#findComment-925309 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.