Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2023-09-02 13:13:02
forward
787 people have browsed it

在多行插入的情况下,对 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
Copy after login

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template