last_insert_id()与SELECT Max(ID) [英] last_insert_id() vs SELECT Max(ID)
问题描述
使用SELECT Max(ID)FROM表比使用SELECT last_insert_id()安全吗?在SELECT last_insert_id()中,它们作为2个单独的查询运行吗?
Is using SELECT Max(ID) FROM table safer than using SELECT last_insert_id(), where they run as 2 separate queries?
我担心在last_insert_id()可以运行之前,将发生另一次插入.
I'm concerned that before the last_insert_id() can run, another insert will take place.
推荐答案
使用SELECT Max(ID)FROM表比使用SELECT last_insert_id()更安全
Is using SELECT Max(ID) FROM table safer than using SELECT last_insert_id()
绝对不会,永远不会! LAST_INSERT_ID()
存在的原因恰恰是您指出的原因:其他客户端可能进行了其他插入. LAST_INSERT_ID()
始终为您提供当前连接上最后插入的ID.
Definitely not, never! LAST_INSERT_ID()
exists exactly for the reason you state: Other clients could have made other inserts. LAST_INSERT_ID()
always gives you the last inserted ID on the current connection.
这篇关于last_insert_id()与SELECT Max(ID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!