ฟังก์ชัน 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));
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)