Versions available for this page:
The LAST_INSERT_ID function returns the value that has been most recently inserted to the AUTO_INCREMENT column by a single INSERT statement. The value returned by the LAST_INSERT_ID function has the following characteristics.
LAST_INSERT_ID()
CREATE TABLE ss (id INT AUTO_INCREMENT NOT NULL PRIMARY KEY, text VARCHAR(32));
INSERT into ss VALUES(NULL,’cubrid’);
SELECT LAST_INSERT_ID();
last_insert_id()
=======================
1
INSERT INTO ss VALUES(NULL,’database’),(NULL,’manager’);
SELECT LAST_INSERT_ID();
last_insert_id()
=======================
3
CREATE TABLE tbl (id INT AUTO_INCREMENT);
INSERT INTO tbl values (500), (NULL), (NULL);
SELECT LAST_INSERT_ID();
last_insert_id()
=======================
1
INSERT INTO tbl values (500), (NULL), (NULL);
SELECT LAST_INSERT_ID();
last_insert_id()
=======================
3
SELECT * FROM tbl;
id
=======================
500
1
2
500
3
4