MYSQL last_insert_id() 和并发

2021-12-28 00:00:00 concurrency mysql

我有一个简单的 MYSQL 问题.如果我在一个网页上运行 INSERT QUERY 之后立即执行包含 LAST_INSERT_ID() 的查询,该网页有许多并发用户访问执行 INSERTLAST_INSERT_ID() 的值是否会被掺假/损坏?

I have a simple MYSQL question. If I make a query that contains LAST_INSERT_ID() right after an INSERT QUERY running on a web page which has many concurrent users accessing other pages that perform INSERT operations would the value of LAST_INSERT_ID() be adulterated/corrupted?

推荐答案

不,它会从当前连接返回插入 id.只要您的脚本没有进行任何其他插入,您就会得到您想要的.

No, it will return the insert id from the current connection. As long as your script hasn't made any other inserts, you will get the one you want.

另请注意,这只会返回生成的 ID(例如自动增量).如果您创建自己的 ID,它不会返回给您.

Also be aware that this will only return a generated ID (e.g. an auto-increment). If you are creating your own ID's it won't return this to you.

相关文章