如何在MySQL中按auto_increment排序?
让我们首先创建一个表-
create table DemoTable(Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,FirstName varchar(100));
使用插入命令在表中插入一些记录-
insert into DemoTable(FirstName) values('Chris'); insert into DemoTable(FirstName) values('Robert'); insert into DemoTable(FirstName) values('David'); insert into DemoTable(FirstName) values('Mike'); insert into DemoTable(FirstName) values('Adam'); insert into DemoTable(FirstName) values('Carol');
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 2 | Robert | | 3 | David | | 4 | Mike | | 5 | Adam | | 6 Carol | +----+-----------+ 6 rows in set (0.00 sec)
以下是按auto_increment排序的查询-
select *from DemoTable order by Id DESC;
这将产生以下输出-
+----+-----------+ | Id | FirstName | +----+-----------+ | 6 | Carol | | 5 | Adam | | 4 | Mike | | 3 | David | | 2 | Robert | | 1 | Chris | +----+-----------+ 6 rows in set (0.00 sec)