选择底部 n 条记录的 MySQL 查询
我们首先创建一个表格 -
mysql> create table DemoTable724 (Value int); Query OK, 0 rows affected (0.79 sec)
使用 insert 命令在表格中插入一些记录 -
mysql> insert into DemoTable724 values(101); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable724 values(183); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable724 values(983); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable724 values(234); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable724 values(755); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable724 values(435); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable724 values(675); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable724 values(345); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable724 values(948); Query OK, 1 row affected (0.14 sec)
使用 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)
广告