查询MySQL数据库以回显最高的自动递增编号?
让我们首先创建一个ID为auto_increment的表-
mysql> create table DemoTable ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserName varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(UserName) values('John'); mysql> insert into DemoTable(UserName) values('Larry'); mysql> insert into DemoTable(UserName) values('Chris'); mysql> insert into DemoTable(UserName) values('Bob'); mysql> insert into DemoTable(UserName) values('Carol'); mysql> insert into DemoTable(UserName) values('David'); mysql> insert into DemoTable(UserName) values('Robert');
以下是使用select语句显示表中所有记录的查询-
mysql> select *from DemoTable;
这将产生以下输出-
+--------+----------+ | UserId | UserName | +--------+----------+ | 1 | John | | 2 | Larry | | 3 | Chris | | 4 | Bob | | 5 | Carol | | 6 | David | | 7 | Robert | +--------+----------+ 7 rows in set (0.00 sec)
这是回显最高自动递增数字的查询-
mysql> select *from DemoTable order by UserId desc limit 0,1;
这将产生以下显示最大自动递增值的输出-
+--------+----------+ | UserId | UserName | +--------+----------+ | 7 | Robert | +--------+----------+ 1 row in set (0.00 sec)