相当于 mySQL 中的 SQLServer 函数 SCOPE_IDENTITY()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/560783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The equivalent of SQLServer function SCOPE_IDENTITY() in mySQL?
提问by kristof
What is the equivalent of SQLServer function SCOPE_IDENTITY()in mySQL?
mySQL中 SQLServer 函数SCOPE_IDENTITY()的等价物是什么?
回答by Sean Bright
Thisis what you are looking for:
这就是你要找的:
LAST_INSERT_ID()
In response to the OP's comment, I created the following bench test:
针对 OP 的评论,我创建了以下基准测试:
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();
This returns:
这将返回:
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 1 |
+------------------+
So it uses the LAST_INSERT_ID()
of the original table and not the table INSERT
ed into inside the trigger.
所以它使用LAST_INSERT_ID()
原始表的 ,而不是INSERT
触发器内部的表。
Edit: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.
编辑:经过这么长时间,我意识到SELECT LAST_INSERT_ID()
我的答案中显示的结果是错误的,尽管最后的结论是正确的。我已将结果更新为正确的值。
回答by Ramgy Borja
open MySqlcommand type SELECT LAST_INSERT_ID();
then ENTER
打开MySql命令类型SELECT LAST_INSERT_ID();
然后输入