如何准确获取mysql中最后插入的记录的ID? [英] How to get exactly the id of the last inserted record in mysql?

查看:104
本文介绍了如何准确获取mysql中最后插入的记录的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Web应用程序,可以同时由多个用户使用.他们可以同时添加数据.

I am building a web application, which can be used by multiple users simultaneously. They can add data at the same time.

我有一个名为Doctor_main的表,如下所示

I have a table named doctor_main as follows

数据库的屏幕快照http://img687.imageshack.us/img687/7033/testxqz.png

一旦添加了有关医生的记录,我希望返回插入记录的ID(这是一个自动递增字段).

Once a record about a doctor is added, I want the id of the inserted record(which is an auto increment field) to be returned.

我知道我可以使用LAST_INSERT_ID()和mysql_insert_id之类的方法.但是我不知道它的表现.

I know i can use methods like LAST_INSERT_ID() and mysql_insert_id. But i don't know how it behaves.

我需要该特定用户插入的记录的确切ID.

I need the exact id of the record which is inserted by that particular user.

有时候,如果两个用户试图插入一条记录,则返回的ID不应交换.

Sometimes, if two users are trying to insert a record, the id which is returned shouldn't get exchanged.

要实现这种功能,我应该使用哪种mysql功能?

To achieve this what kind of mysql function should i use ?

推荐答案

手册中有专门针对此主题的整页内容:

There's a whole page in the manual dedicated to this subject:

以下是该页面的引文,应有助于回答您的问题:

Here's a quote from that page that should help answer your question:

对于LAST_INSERT_ID(),将在每个连接的基础上在服务器中维护最近生成的ID.不会被其他客户端更改.

For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client.

这篇关于如何准确获取mysql中最后插入的记录的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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