last_insert_id()与SELECT Max(ID) [英] last_insert_id() vs SELECT Max(ID)

查看:292
本文介绍了last_insert_id()与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.

mySQL参考

这篇关于last_insert_id()与SELECT Max(ID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆