CreatetableStudent(IdINTPRIMARYKEYNOTNULLAUTO_INCREMENT,NameVarchar(5));QueryOK,0ro"/> CreatetableStudent(IdINTPRIMARYKEYNOTNULLAUTO_INCREMENT,NameVarchar(5));QueryOK,0ro">

Home >Database >Mysql Tutorial >What is the use of the MySQL LAST_INSERT_ID() function?

What is the use of the MySQL LAST_INSERT_ID() function?

WBOY
WBOYforward
2023-09-20 13:41:02813browse

MySQL LAST_INSERT_ID() 函数有什么用?

MySQL LAST_INSERT_ID() function is used to obtain the recently generated sequence number through AUTO_INCRMENT.

Example

In this example, we are creating a table named "Student" that has the AUTO_INCRMENT column. We insert two values ​​in the "Name" column and when we use the INSERT_LAST_ID() function, it returns the most recently generated sequence number, which is 2.

mysql> Create table Student(Id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, Name Varchar(5));
Query OK, 0 rows affected (0.13 sec)

mysql> Insert into student(Name) Values('Raman');
Query OK, 1 row affected (0.06 sec)

mysql> Insert into student(Name) Values('Rahul');
Query OK, 1 row affected (0.07 sec)

mysql> Select* from student;
+----+-------+
| Id | Name |
+----+-------+
| 1 | Raman |
| 2 | Rahul |
+---+-------+
2 rows in set (0.00 sec)

mysql> Select Last_insert_id();
+------------------+
| Last_insert_id() |
+------------------+
|                2 |
+------------------+
1 row in set (0.00 sec)

The above is the detailed content of What is the use 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