如何查詢 MySQL AUTO_INCREMENT 最近分配的序列號?


Last_Insert_Id() MySQL 函式用於查詢 AUTO_INCREMENT 最近分配的序列號。

示例

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

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

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

mysql> Select* from Employee;
+----+---------+
| Id | Name    |
+----+---------+
| 1  |Harvinder|
| 2  | Suresh  |
+----+---------+
2 rows in set (0.00 sec)

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

更新時間: 2020-01-30

355 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

開始使用
廣告
© . All rights reserved.