IlaminiAyebatonyeDagogo Posted January 6, 2014 Share Posted January 6, 2014 Please i assistance in selecting the last 8 inserted value into a database. For instance I want to select the last 8 post insterted into database *test* table *forum * field (*). Explaing : what i need is the mysql or mysqlite code to select the last 8 value in database test table forum and all the data we have in that field. Thank you all in advance Quote Link to comment Share on other sites More sharing options...
dalecosp Posted January 6, 2014 Share Posted January 6, 2014 Show us the table structure: mysql> describe mytablename; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | bar | text | YES | | NULL | | +-------+---------+------+-----+---------+----------------+ Quote Link to comment Share on other sites More sharing options...
Dowlat Posted January 6, 2014 Share Posted January 6, 2014 Mysql offers the OFFSET in conjunction with LIMIT, that is the quickest method. Quote Link to comment Share on other sites More sharing options...
Ram_Saw Posted January 6, 2014 Share Posted January 6, 2014 I have the same problem, but I need then the posts from 8 to 16 and etc. So if it isn't difficult explain please the fastest method of getting this info. Quote Link to comment Share on other sites More sharing options...
IlaminiAyebatonyeDagogo Posted January 7, 2014 Author Share Posted January 7, 2014 the tables contain a id auto increment field as index, a username field varchar, a body longtext field to store post, a subject field longtext as well Quote Link to comment Share on other sites More sharing options...
IlaminiAyebatonyeDagogo Posted January 7, 2014 Author Share Posted January 7, 2014 table name is forum Quote Link to comment Share on other sites More sharing options...
GetFreaky Posted January 7, 2014 Share Posted January 7, 2014 (edited) SELECT * FROM `table_name` ORDER BY `table_name.id` DESC LIMIT 0 , 5 ^^^ This isn't safe because if your manipulating data, editing forums, and adding new entries you may get some conflicts especially if you have a DATE field in the future, but since you explained your table's structure and definition, I guess this should be fine. Concerns are that even if the ID is unique, it being sequential and increasing isn't guaranteed. Another way is to get the total amount of rows then do LIMIT ($max_rows - 5),$max_rows You can also take advantage of Mysql's Local variable declarations and store the total rows, and do the latter method, instead of 2 queries, 1 being for $max_rows, and the other for the query. Edited January 7, 2014 by GetFreaky Quote Link to comment 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.