Jump to content

How to select the last eight inserted value into a database.


Recommended Posts

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

Link to comment
Share on other sites

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    |                |
+-------+---------+------+-----+---------+----------------+
Link to comment
Share on other sites

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 by GetFreaky
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.