Outdated Version

You are viewing an older version of this section. View current production version.

LAST_INSERT_ID

Returns the last value inserted into an AUTO_INCREMENT column.

LAST_INSERT_ID()

Return type

Integer

Examples

After a multi-insert statement, the value returned by LAST_INSERT_ID() is the first value inserted in that group of records.

memsql> create table persons (
      -> id bigint primary key auto_increment,
      -> firstname varchar(64),
      -> lastname varchar(64));

memsql> insert into persons values
      -> (NULL, 'Eponymous', 'Bach');

memsql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                1 |
+------------------+

memsql> insert into persons values
      -> (NULL, 'Ping', 'Baudot'),
      -> (NULL, 'Count', 'Modulo'),
      -> (NULL, 'Hugh', 'Rustic');

memsql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                2 |
+------------------+
1 row in set (0.00 sec)