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

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    |                |
+-------+---------+------+-----+---------+----------------+
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.

Archived

This topic is now archived and is closed to further replies.

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