3

LAST_INSERT_ID() returns the last inserted ID from column with AUTO_INCREMENT property.

Firstly, is there any way to get the last inserted ID at the column which has not AUTO_INCREMENT property.

If yes, how do i get the last inserted ID?

Any ideas would be awesome.

1
  • Note that LAST_INSERT_ID is session-specific Commented Jul 14, 2013 at 20:14

2 Answers 2

6

Not always guaranteed, but you could use MAX():

SELECT MAX(ID) FROM YourTable 

The reason this might not be guaranteed is if you are having multiple transactions running at the same time, this could potentially select the incorrect id...

Similarly, ORDER BY with LIMIT:

SELECT ID FROM YourTable ORDER BY ID Desc Limit 1 
Sign up to request clarification or add additional context in comments.

Comments

1

Does your id is Primary key?

You can check timestamp value if you have it.

In fact there are no way of getting last inserted row without autoincrement or timestamp. In case you can not change main table you can create other table and map autoincrement to the id you inserted.

1 Comment

Yes it is Primary Key. For more info please comment it on first post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.