PDO 获取多个插入 ID

发布于 2024-11-05 16:49:28 字数 302 浏览 6 评论 0原文

使用 PDO 运行以下查询(实际上,我使用准备好的语句,但有同样的问题)

INSERT INTO MyTable(MyField) VALUES('Row1'), ('Row2')

如何获取与 Row1Row2 相关的记录?

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何处潇湘 2024-11-12 16:49:28

如果您使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文