Name

LAST_INSERT_ID( )

Synopsis

LAST_INSERT_ID([id])

This function returns the identification number of the last row inserted using the MySQL connection. The identification number for rows inserted by other clients will not be returned to the other connections. Identification numbers that were set manually when rows are inserted, without the aid of AUTO_INCREMENT, won’t register and therefore won’t be returned by LAST_INSERT_ID( ). If multiple rows are inserted by one SQL statement, LAST_INSERT_ID( ) returns the identification number for the first row inserted.

SELECT LAST_INSERT_ID( );
+------------------+
| LAST_INSERT_ID( ) |
+------------------+
|             1039 |
+------------------+

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.