且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检索多行最后插入的ID

更新时间:2023-12-03 13:46:04

调用last_insert_id()会为您提供最后一批中插入的FIRST行的ID.插入的所有其他字符都保证是连续的.

Calling last_insert_id() gives you the id of the FIRST row inserted in the last batch. All others inserted, are guaranteed to be sequential.

除非您做的事情很奇怪,否则您可以轻松地计算出每一行的ID.

Unless you're doing something very strange, this allows you to calulate the ID of each row easily enough.

实际上,该行为在5.1中有所不同,具体取决于innodb自动递增模式参数的设置;这无关紧要.只要您不更改默认值,就会看到预期的行为.

Actually the behaviour varies in 5.1 depending on the setting of the innodb auto increment mode parameter; this should not matter. As long as you don't change it from the default, you will see the expected behaviour.

在某些情况下,这并没有达到您的期望并且没有用-例如,如果您执行ON DUPLICATE KEY UPDATE或INSERT IGNORE.在这种情况下,您需要做其他事情才能得出每一行的ID.

There are occasional cases where this doesn't do what you expect and is not useful - such as if you do an ON DUPLICATE KEY UPDATE or INSERT IGNORE. In these cases, you'll need to do something else to work out the IDs of each row.

但是对于普通的INSERT批处理,没有为auto-inc列指定值,这很容易.

But for a plain vanilla INSERT batch, with no values specified for the auto-inc column, it's easy.

有关自动增量的完整说明在innodb中处理是在这里