Mysql批量插入不更新LAST_INSERT_ID

Mysql Batch insert doesn't update LAST_INSERT_ID

我找到了一个有趣的解决方案,解决了我在这个 Whosebug 问题的第一个答案中遇到的问题:1st answer 问题是它没有像我预期的那样工作。如果我的 table 为空,则第一个批量插入工作正常,但 LAST_INSERT_ID returns 1 当批量插入 100 行时。如果我更改它并放入 100 个简单的 Insert 就可以了。 autoinc_loc_mode 设置为 1。

是否有要更改的设置?

您遇到的是预期行为; from MySQL docs:

If you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only. The reason for this is to make it possible to reproduce easily the same INSERT statement against some other server.