LAST_INSERT_ID

说明

返回上一次执行Insert的ID。

语法

last_insert_id ::=

参数

不支持带参数的 LAST_INSERT_ID 函数。

返回值

INT8

LAST_INSERT_ID 自动返回最后一个INSERT的AUTO_INCREMENT列的值。 注:(如果由于unique 或者primary key冲突导致插入不成功,则auto_increment计数器不会变化,此时调用last_insert_id()返回值不变,表示没有新行被插入。)

示例

示例1: 使用 LAST_INSERT_ID求最后一个INSERT的AUTO_INCREMENT列的值

--  使用 last_insert_id 获取上次自动insert的id
create table t1(a int auto_increment unique,b int );
insert into t1(b) values (2);
insert into t1(b) values (4);

select * from t1;
A(int)      |B(int)      |
------------------------
1           |2           |
------------------------
2           |4           |
总数目:2

select last_insert_id();
LAST_INSERT_ID(bigint)      |
----------------------------
2                           |
总数目:1

对于AUTO_INCREMENT列,假如使用一条INSERT语句插入多个行,LAST_INSERT_ID 只返回插入的第一行数据时产生的值。其原因是这使依靠其它服务器复制同样的INSERT语句变得简单。

示例2: 使用 LAST_INSERT_ID 求一次插入多行后AUTO_INCREMENT列的值

--  使用last_insert_id获取一次插入多行后的id
insert into t1(b) values (6),(8),(10);

select * from t1;
A(int)      |B(int)      |
------------------------
1           |2           |
------------------------
2           |4           |
------------------------
3           |6           |
------------------------
4           |8           |
------------------------
5           |10          |
总数目:5

select last_insert_id();
LAST_INSERT_ID(bigint)      |
----------------------------
3                           |
总数目:1

对于SERIAL列,使用一条INSERT语句插入多个行,LAST_INSERT_ID 返回插入最后一行产生的数据。

示例3: 使用 LAST_INSERT_ID 求一次插入多行后SERIAL列的值

--  使用last_insert_id获取一次插入多行后的id
drop table t2;
create table t2(a serial,b int );
insert into t2(b) values (1);
insert into t2(b) values (2);
insert into t2(b) values (3),(4),(5);

select * from t2;
A(int)      |B(int)      |
------------------------
1           |1           |
------------------------
2           |2           |
------------------------
3           |3           |
------------------------
4           |4           |
------------------------
5           |5           |
总数目:5

select last_insert_id();
LAST_INSERT_ID(bigint)      |
----------------------------
5                           |
总数目:1
drop table t2;

客户端每次连接后 LAST_INSERT_ID 产生的ID保存在服务器中,与table无关,是session级别的。即如果向表a插入数据后,再向表b插入数据,LAST_INSERT_ID返回表b中的Id值。 这意味着函数向一个给定客户端返回的值是该客户端产生对影响AUTO_INCREMENT列的最新语句第一个 AUTO_INCREMENT值。这个值不会被其它客户端影响。

示例4: 使用 LAST_INSERT_ID 求不同table中AUTO_INCREMENT列的值

--  使用不同的table获取session中上次insert之后的id,进行update。
create table t2(a int auto_increment unique,b int );
insert into t2(b) values (1);
insert into t2(b) values (3);
insert into t2(b) values (4);
insert into t2(b) values (5);
insert into t2(b) values (6);
insert into t2(b) values (7);

select * from t2;
A(int)      |B(int)      |
------------------------
1           |1           |
------------------------
2           |3           |
------------------------
3           |4           |
------------------------
4           |5           |
------------------------
5           |6           |
------------------------
6           |7           |
总数目:6

select last_insert_id();
LAST_INSERT_ID(bigint)      |
----------------------------
6                           |
总数目:1
update t1 set a = last_insert_id() where b = 2;

select *from t1 where b=2;
A(int)      |B(int)      |
------------------------
6           |2           |
总数目:1