小编典典

MySQL获取特定表的最后一个ID

sql

我必须从特定的插入表中获取最后一个插入ID吗?可以说我有以下代码:

INSERT INTO blahblah (test1, test 2) VALUES ('test1', 'test2');
INSERT INTO blahblah2 (test1, test 2) VALUES ('test1', 'test2');
INSERT INTO blahblah3 (test1, test 2, lastid) VALUES ('test1', 'test2', last id of blahblah);

如何获取表blahblah3中表blahblah的插入ID?LAST_INSERT_ID()仅给您最后一个插入ID

问候西蒙:)


阅读 285

收藏
2021-04-15

共1个答案

小编典典

您可以使用
LAST_INSERT_ID()
函数。试试这个:

INSERT INTO blahblah (test1, test2) VALUES ('test1', 'test2');

SELECT LAST_INSERT_ID() INTO @blahblah;

INSERT INTO blahblah2 (test1, test2) VALUES ('test1', 'test2');

INSERT INTO blahblah3 (test1, test2, lastid) VALUES ('test1', 'test2', @blahblah);
2021-04-15