[Switch] Detailed explanation of the function use of LAST_INSERT_ID() in Mysql

Reprinted from: http://www.jb51.net/article/62973.htm

Recently, I was working on a high-quality course project with Sobin. Because a fixed id was used as the association between tables, after inserting data in the previous table, the self-incrementing id generated by the inserted data should be passed to the next table. After some research, I decided to use Mysql to provide a LAST_INSERT_ID() function.

 

copy code code show as below:

LAST_INSERT_ID() (with no argument) returns the first automatically generated value that was set for an AUTO_INCREMENT column by the most recently executed INSERT or UPDATE statement to affect such a column. For example, after inserting a row that generates an AUTO_INCREMENT value, you can get the value like this:
mysql> SELECT LAST_INSERT_ID();
-> 195

 

Simply put, this function will return the value of the field that is automatically incremented in the table for the inserted record. Generally, we name the auto-increment field ID. This will return the ID value of the record that was just inserted.

A simple example:

 

copy code code show as below:

$query="INSERT INTO `testtable` (`clou1`,`clou2`) VALUES ('testvalue','test')";
mysql_query($query);
$query="SELECT LAST_INSERT_ID()";
$result=mysql_query($query);
$rows=mysql_fetch_row($result);
echo $rows[0];

 

This function is based on connection, that is, it will not be affected by the connection of other clients, so the result is accurate. If you use select max(id) from table, under high-density insert requests, there may be problems and return an error value

Description of LAST_INSERT_ID

As can be seen from the name, LAST_INSERT_ID is the last inserted ID value. According to MySQL's official manual, there are two ways to use it.

One is without parameters: LAST_INSERT_ID(), this method is used together with the AUTO_INCREMENT attribute. When a new record is added to a table with an 'AUTO_INCREMENT' attribute field, LAST_INSERT_ID() returns the value of the field. You can try it (I have verified it);
the second is with an expression: as described above, LAST_INSERT_ID(value+1), it returns the value of the expression, ie 'value+1';
######### #########################
LAST_INSERT_ID() Automatically returns the value that occurred in the first table set by the AUTO_INCREMENT column in the last INSERT or UPDATE query.

MySQL的LAST_INSERT_ID的注意事项:

第一、查询和插入所使用的Connection对象必须是同一个才可以,否则返回值是不可预料的。

mysql> SELECT LAST_INSERT_ID();

        -> 100

使用这函数向一个给定Connection对象返回的值是该Connection对象产生对影响AUTO_INCREMENT列的最新语句第一个AUTO_INCREMENT值的。这个值不能被其它Connection对象的影响,即它们产生它们自己的AUTO_INCREMENT值。

第二、LAST_INSERT_ID 是与table无关的,如果向表a插入数据后,再向表b插入数据,LAST_INSERT_ID返回表b中的Id值。

第三、 假如你使用一条INSERT语句插入多个行,  LAST_INSERT_ID() 只返回插入的第一行数据时产生的值。其原因是这使依靠其它服务器复制同样的 INSERT语句变得简单。

mysql> INSERT INTO t VALUES

    -> (NULL, ‘Mary'), (NULL, ‘Jane'), (NULL, ‘Lisa');

mysql> SELECT * FROM t;

| id | name |

+—-+——+

|  1 | Bob  |

|  2 | Mary |

|  3 | Jane |

|  4 | Lisa |

mysql> SELECT LAST_INSERT_ID();  //这就是我要说明的关键问题。

| LAST_INSERT_ID() |

|                2 |

虽然将3 个新行插入 t, 对这些行的第一行产生的 ID 为 2, 这也是 LAST_INSERT_ID()返回的值。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326757309&siteId=291194637