MySQL查询以获取AUTO_INCREMENT字段的下一个数字?
让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT, -> PRIMARY KEY(Id) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(); mysql> insert into DemoTable values(); mysql> insert into DemoTable values();
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
这将产生以下输出-
+----+ | Id | +----+ | 1 | | 2 | | 3 | +----+ 3 rows in set (0.00 sec)
以下是查询以按顺序获取AUTO_INCREMENT字段的下一个数字-
mysql> SELECT auto_increment FROM information_schema.tables WHERE table_name='DemoTable';
输出结果
这将产生以下输出-
+----------------+ | AUTO_INCREMENT | +----------------+ | 4 | +----------------+ 1 row in set (0.26 sec)