MySQL LAST_INSERT_ID() 与多记录 INSERT 语句一起使用
如果我使用执行单个记录插入的循环插入多个记录,则返回的最后一个插入 ID 正如预期的那样,是最后一个。但是,如果我执行多条记录插入语句:
INSERT INTO people (name,age)
VALUES ('William',25), ('Bart',15), ('Mary',12);
假设上面的三个是表中插入的第一条记录。在插入语句之后,我期望最后一个插入 id 返回 3,但它返回 1。相关语句的第一个插入 id。
那么有人可以确认这是否是多记录 INSERT 语句上下文中 LAST_INSERT_ID()
的正常行为。所以我可以基于它来编写我的代码。
If I insert multiple records with a loop that executes a single record insert, the last insert id returned is, as expected, the last one. But if I do a multiple records insert statement:
INSERT INTO people (name,age)
VALUES ('William',25), ('Bart',15), ('Mary',12);
Let's say the three above are the first records inserted in the table. After the insert statement I expected the last insert id to return 3, but it returned 1. The first insert id for the statement in question.
So can someone please confirm if this is the normal behavior of LAST_INSERT_ID()
in the context of multiple records INSERT statements. So I can base my code on it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。
last_insert_id()
的这种行为是 MySQL 文档中记录:Yes. This behavior of
last_insert_id()
is documented in the MySQL docs:手册页中提到了此行为MySQL。它在评论中,但没有受到质疑,所以我猜测这是预期的行为。
This behavior is mentioned on the man page for MySQL. It's in the comments but is not challenged, so I'm guessing it's the expected behavior.
我认为如果你的表有唯一的自动增量列(ID)并且你不要求它们由mysql本身返回,这是可能的。我会花费你 3 个以上的数据库请求和一些处理。需要执行以下步骤:
使用数据进行多个 INSERT ... VALUES () 查询并保留它们:
在插入后立即获取“After MAX(ID)”:
获取 ID 介于“Before MAX(ID)”和“After MAX(ID)”之间的记录,包括:
使用您插入的数据检查检索到的数据以匹配它们并删除任何记录不是您插入的。其余记录有您的 ID:
这就是普通人在现实世界中使用部分代码解决该问题的方法。由于自动增量,它应该获得尽可能少的记录来检查,因此它们不会进行大量处理。这不是最终的“复制和粘贴”代码 - 例如。您必须根据您的需要创建自己的函数compare_content()。
I think it's possible if your table has unique autoincrement column (ID) and you don't require them to be returned by mysql itself. I would cost you 3 more DB requests and some processing. It would require these steps:
Make multiple INSERT ... VALUES () query with your data and keep them:
Get "After MAX(ID)" right after your insert:
Get records with IDs between "Before MAX(ID)" and "After MAX(ID)" including:
Do a check of retrieved data with data you inserted to match them and remove any records that were not inserted by you. The remaining records have your IDs:
This is just how a common person would solve it in a real world, with parts of code. Thanks to autoincrement it should get smallest possible amount of records to check against, so they will not take lot of processing. This is not the final "copy & paste" code - eg. you have to create your own function compare_content() according you your needs.