Select the last inserted rows, latest IDs

Discuss coding issues, and scripts related to PHP and MySQL.
Marius
Posts:107

Select the last inserted rows, latest IDs

Hi
I have a table in mysql database, with 3 columns: id | title | description .
The id column is INT AUTO_INCREMENT .
Does anyone know how to select the last two inserted rows, with the latest inserted IDs?

Admin Posts:805
Hi,
Use these instructions in the SELECT query: ORDER BY ID DESC and LIMIT.

Code: Select all

SELECT * FROM table ORDER BY id DESC LIMIT 2
- ORDER BY ID DESC - will sort your records in descending order, according to the id.
- LIMIT 2 - will return you two (and the first two) records.

sandy Posts:3
Hi,
If you're using PDO, use PDO::lastInsertId();// To Display the last Inserted value
If you're using MySql, use mysql_insert_id();// To Display the last Inserted value

Thanks

Edit:
- These work after an INSERT query is performed.