是的,您可以使用SELECT AUTO_INCREMENT找出下一个auto_increment,如以下语法所示-
SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA= yourDatabaseName AND TABLE_NAME=yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable ( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(20), ClientAge int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(ClientName,ClientAge) values('John',23); mysql> insert into DemoTable(ClientName,ClientAge) values('Carol',21); mysql> insert into DemoTable(ClientName,ClientAge) values('Bob',24); mysql> insert into DemoTable(ClientName,ClientAge) values('David',25);
以下是使用select语句显示表中所有记录的查询-
mysql> select *from DemoTable;
这将产生以下输出-
+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ | 1 | John | 23 | | 2 | Carol | 21 | | 3 | Bob | 24 | | 4 | David | 25 | +----------+------------+-----------+ 4 rows in set (0.00 sec)
这是查询以找出上表中要使用的下一个auto_increment。
mysql> SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA= 'sample' AND TABLE_NAME='DemoTable';
这将产生以下输出-
+----------------+ | AUTO_INCREMENT | +----------------+ | 5 | +----------------+ 1 row in set (0.20 sec)