MySQL LAST_INSERT_ID()函数返回最新生成的序列号,但是如果有多个行插入,它将返回由最前面插入的行生成的序列号。
示例
mysql> Insert into Student(Name) values('Ram'),('Mohan'),('Aryan');
Records: 3 Duplicates: 0 Warnings: 0
上面的查询借助多行插入查询在Student表中插入了三个值。列’Id’的值可以在以下查询的帮助下进行检查-
mysql> Select * from Student;
+----+-------+
| Id | Name |
+----+-------+
| 1 | Raman |
| 2 | Rahul |
| 3 | Ram |
| 4 | Mohan |
| 5 | Aryan |
+----+-------+
5 rows in set (0.00 sec)
这意味着Last_Insert_Id()必须返回5作为输出,但是正如我们看到的那样,它返回值3如下:
mysql> Select Last_Insert_Id();
+------------------+
| Last_Insert_Id() |
+------------------+
| 3 |
+------------------+
1 row in set (0.00 sec)
它返回值3,因为3是具有多个插入行查询的最前面插入行的值。
正文完