PDO 获取多个插入 ID
使用 PDO 运行以下查询(实际上,我使用准备好的语句,但有同样的问题)
INSERT INTO MyTable(MyField) VALUES('Row1'), ('Row2')
如何获取与 Row1
和 Row2
相关的记录?
$db->lastInsertId()
字面上返回最后一个 Id。
获取最后一个 ID,减去记录数并假设范围涵盖我的所有记录是否足够?是否可以有间隙/跳跃。这个查询保证是原子的吗?
Running the following query using PDO (Actually, I use prepared statements but same problem)
INSERT INTO MyTable(MyField) VALUES('Row1'), ('Row2')
How can I get the Ids for the records relating to Row1
and Row2
?
$db->lastInsertId()
literally returns the last single Id.
Is it sufficient to take this last Id, subtract the # of records and assume that range covers all my records? can there be gaps/jumps. Is this query guaranteed to be atomic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 MyISAM 表,那么由于表级锁定机制,您只能获得给定的 id 范围。
阅读 http 后://dev.mysql.com/doc/refman/5.5/en/innodb-auto-increment-handling.html#innodb-auto-increment-traditional假设您使用的是“传统”innodb锁定(可能是这样)那么对于单个语句来说,ID 集将是连续的。
If you're using MyISAM tables then because of the table level locking mechanism it's only possible for you to get given a range of ids.
After reading http://dev.mysql.com/doc/refman/5.5/en/innodb-auto-increment-handling.html#innodb-auto-increment-traditional assuming you're using "traditional" innodb locking (which likely you are) then again for a single statement the set of IDs will be sequential.