MySQL LAST_INSERT_ID()函数用于通过AUTO_INCREMENT获取最新生成的序列号。
在此示例中,我们将创建一个名为“ Student”的表,该表具有一个AUTO_INCREMENT列。我们在“名称”列中插入两个值,当我们使用INSERT_LAST_ID()函数时,它将返回最近生成的序列号,即2。
mysql> Create table Student(Id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, Name Varchar(5)); mysql> Insert into student(Name) Values('Raman'); mysql> Insert into student(Name) Values('Rahul'); 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)