The equivalent of SQLServer function SCOPE_IDENTITY() in mySQL?(相当于 mySQL 中的 SQLServer 函数 SCOPE_IDENTITY()?)
问题描述
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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:相当于 mySQL 中的 SQLServer 函数 SCOPE_IDENTITY()?


基础教程推荐
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01