1

I have a table in database which has a primary key called id. Now i want to display the most recently added 2 records into the table.

0

3 Answers 3

5

Assuming id is some sort of auto-incrementing integer value then the following will work

SELECT * FROM table ORDER BY id DESC LIMIT 2;

If you want just the last inserted record id (again assuming the insert generates an autoincrement id) there is also LAST_INSERT_ID. But beware this is global and will return last inserted id database-wide so it is not often used in SELECTS but rather as an OUT paramer in a routine to return the id of the just-inserted row.

SELECT LAST_INSERT_ID();
Sign up to request clarification or add additional context in comments.

Comments

2

This wil definitely work,

SELECT * FROM table_name ORDER BY id DESC,LIMIT 2.

Comments

0

Try,

SELECT * FROM `table`
ORDER BY id 
DESC 
LIMIT 0, 2;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.