PDO获取多个插入ID [英] PDO Get Multiple Insert Ids
问题描述
使用PDO运行以下查询(实际上,我使用准备好的语句,但存在相同的问题)
Running the following query using PDO (Actually, I use prepared statements but same problem)
INSERT INTO MyTable(MyField) VALUES('Row1'), ('Row2')
如何获取与Row1
和Row2
有关的记录的ID?
How can I get the Ids for the records relating to Row1
and Row2
?
$db->lastInsertId()
从字面上返回最后一个ID.
$db->lastInsertId()
literally returns the last single Id.
采用这个最后的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?
推荐答案
如果您使用的是MyISAM表,则由于表级锁定机制,只能给您指定范围的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.
这篇关于PDO获取多个插入ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!