mySQL 中 SQLServer 函数 SCOPE_IDENTITY() 的等价物是什么?
What is the equivalent of SQLServer function SCOPE_IDENTITY() in mySQL?
这是您要找的:
LAST_INSERT_ID()
为了回应 OP 的评论,我创建了以下基准测试:
In response to the OP's comment, I created the following bench test:
CREATE TABLE Foo
(
FooId INT AUTO_INCREMENT PRIMARY KEY
);
CREATE TABLE Bar
(
BarId INT AUTO_INCREMENT PRIMARY KEY
);
INSERT INTO Bar () VALUES ();
INSERT INTO Bar () VALUES ();
INSERT INTO Bar () VALUES ();
INSERT INTO Bar () VALUES ();
INSERT INTO Bar () VALUES ();
CREATE TRIGGER FooTrigger AFTER INSERT ON Foo
FOR EACH ROW BEGIN
INSERT INTO Bar () VALUES ();
END;
INSERT INTO Foo () VALUES (); SELECT LAST_INSERT_ID();
返回:
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 1 |
+------------------+
所以它使用原始表的 LAST_INSERT_ID()
而不是 INSERT
插入触发器内部的表.
So it uses the LAST_INSERT_ID()
of the original table and not the table INSERT
ed into inside the trigger.
我意识到我的答案中显示的 SELECT LAST_INSERT_ID()
的结果是错误的,尽管最后的结论是正确的.我已将结果更新为正确的值.
I realized after all this time that the result of the SELECT LAST_INSERT_ID()
shown in my answer was wrong, although the conclusion at the end was correct. I've updated the result to be the correct value.
这篇关于相当于 mySQL 中的 SQLServer 函数 SCOPE_IDENTITY()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!