Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the last insert ID from a table

Tags:

I want to get the value of the last ID insert in a table. How I can do this?

like image 403
Enrique San Martín Avatar asked Jun 21 '10 19:06

Enrique San Martín


People also ask

How do I get the last insert ID from a specific table?

If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL. Insert some records in the table using insert command. Display all records from the table using select statement.

How do I get the last inserted ID in SQL?

SELECT @@IDENTITY @@IDENTITY will return the last identity value entered into a table in your current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope.

What is last insert ID?

The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted or updated in a table.


1 Answers

Well the solution that I use is:

select id from NEW TABLE (insert into (val1, val2, ...) values ('lorem', 'ipsum', ...)) 

This gets the id column from the last row inserted in the DB :)

like image 121
Enrique San Martín Avatar answered Oct 06 '22 13:10

Enrique San Martín