您可以为此使用子查询。让我们首先创建一个表-
mysql> create table DemoTable ( Id int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(100); mysql> insert into DemoTable values(115); mysql> insert into DemoTable values(200); mysql> insert into DemoTable values(140); mysql> insert into DemoTable values(124); mysql> insert into DemoTable values(198); mysql> insert into DemoTable values(156); mysql> insert into DemoTable values(178); mysql> insert into DemoTable values(156);
以下是使用select语句显示表中所有记录的查询-
mysql> select *from DemoTable;
这将产生以下输出-
+------+ | Id | +------+ | 100 | | 115 | | 200 | | 140 | | 124 | | 198 | | 156 | | 178 | | 156 | +------+ 9 rows in set (0.00 sec)
这是获取按ID排序的最后5条记录的查询-
mysql> select *from(select *from DemoTable order by Id DESC limit 5)tbl order by Id;
这将产生以下输出-
+------+ | Id | +------+ | 156 | | 156 | | 178 | | 198 | | 200 | +------+ 5 rows in set (0.00 sec)