MySQL查询选择底部的n条记录
让我们首先创建一个表-
mysql> create table DemoTable724 (Value int);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable724 values(101); mysql> insert into DemoTable724 values(183); mysql> insert into DemoTable724 values(983); mysql> insert into DemoTable724 values(234); mysql> insert into DemoTable724 values(755); mysql> insert into DemoTable724 values(435); mysql> insert into DemoTable724 values(675); mysql> insert into DemoTable724 values(345); mysql> insert into DemoTable724 values(948);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable724;
这将产生以下输出-
+-------+ | Value | +-------+ | 101 | | 183 | | 983 | | 234 | | 755 | | 435 | | 675 | | 345 | | 948 | +-------+ 9 rows in set (0.00 sec)
以下是选择底部n条记录的查询。在这里,我们在DESC中订购后选择了4条记录。这将从初始值中获取我们的最后4条记录-
mysql> select *from DemoTable724 order by Value DESC LIMIT 0,4;
这将产生以下输出-
+-------+ | Value | +-------+ | 983 | | 948 | | 755 | | 675 | +-------+ 4 rows in set (0.00 sec)