Home  >  Article  >  Database  >  In the case of multiple row insertions, what is the impact on the output of the MySQL LAST_INSERT_ID() function?

In the case of multiple row insertions, what is the impact on the output of the MySQL LAST_INSERT_ID() function?

WBOY
WBOYforward
2023-09-02 13:13:02699browse

在多行插入的情况下,对 MySQL LAST_INSERT_ID() 函数的输出有何影响?

As we all know, the MySQL LAST_INSERT_ID() function returns the latest generated sequence number, but in the case of multi-row insertion, it will return the row inserted by the most front Generated serial number.

Example

mysql> Insert into Student(Name) values('Ram'),('Mohan'),('Aryan');
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0

The above query inserts three values ​​into the Student table with the help of a multi-row insert query. The value of the column "Id" can be checked with the help of the following query -

mysql> Select * from Student;

+----+-------+
| Id | Name  |
+----+-------+
| 1 | Raman  |
| 2 | Rahul  |
| 3 | Ram    |
| 4 | Mohan  |
| 5 | Aryan  |
+----+-------+

5 rows in set (0.00 sec)

This means that Last_Insert_Id() must return 5 as output, but we can see that it returns the value 3 as follows -

mysql> Select Last_Insert_Id();

+------------------+
| Last_Insert_Id() |
+------------------+
| 3                |
+------------------+

1 row in set (0.00 sec)

It returns the value 3 because 3 is the value of the first inserted row in the above multi-row insert query.

The above is the detailed content of In the case of multiple row insertions, what is the impact on the output of the MySQL LAST_INSERT_ID() function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete