MySQL LAST_INSERT_ID() 函數用于通過 AUTO_INCRMENT 獲取最近生成的序列號。
示例
在這個示例中,我們是創建一個名為“Student”的表,該表具有 AUTO_INCRMENT 列。我們在“Name”列中插入兩個值,當我們使用 INSERT_LAST_ID() 函數時,它會返回最近生成的序列號,即 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)
登錄后復制
以上就是MySQL LAST_INSERT_ID() 函數有什么用?的詳細內容,更多請關注www.92cms.cn其它相關文章!