Kyle123 Posted March 1, 2012 Share Posted March 1, 2012 How can I select the last 5 db items? I was thinking a timestamp or some thing. Quote Link to comment https://forums.phpfreaks.com/topic/258053-last-db-items/ Share on other sites More sharing options...
AyKay47 Posted March 1, 2012 Share Posted March 1, 2012 You mean the last 5 rows? A combination of ORDER BY and LIMIT would most likely be the solution, however we would need to see the db table setup. Quote Link to comment https://forums.phpfreaks.com/topic/258053-last-db-items/#findComment-1322782 Share on other sites More sharing options...
Psycho Posted March 1, 2012 Share Posted March 1, 2012 What he said. But, you will need a field to use for ordering the records to achieve this. If there is an auto-incrementing ID field you can use that. Otherwise you would probably want to create a timestamp field that is automatically populated when the record is created. If you have an existing field of either type you can use that. If not, you can add either one such that you wouldn't have to change any of your existing code since they would be handled automatically by the database. The query to get what you need would look something like this SELECT field1, field2, field3 FROM table_name ORDER BY order_field DESC LIMIT 5 Where 'order_field' is the auto-incrementing int or timestamp field. Quote Link to comment https://forums.phpfreaks.com/topic/258053-last-db-items/#findComment-1322795 Share on other sites More sharing options...
Pikachu2000 Posted March 1, 2012 Share Posted March 1, 2012 As long as you have the timestamp field as a UNIX timestamp, or a valid YYYY-MM-DD HH:MM:SS format: ORDER BY timestamp_field DESC LIMIT 5 Quote Link to comment https://forums.phpfreaks.com/topic/258053-last-db-items/#findComment-1322796 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.