从 MySQL 数据库按 DESC 顺序显示记录同时跳过一些记录?


让我们首先创建一个表 -

mysql> create table DemoTable
(
   Id int,
   Name varchar(100)
);
Query OK, 0 rows affected (0.94 sec)

使用 insert 命令在表中插入一些记录 -

mysql> insert into DemoTable values(10,'Chris');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(1,'David');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(4,'Bob');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(6,'Sam');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(7,'Mike');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(5,'Carol');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values(9,'John');
Query OK, 1 row affected (0.19 sec)

使用 select 语句从表中显示所有记录 -

mysql> select *from DemoTable;

这将产生以下输出 -

+------+-------+
| Id   | Name |
+------+-------+
|   10 | Chris |
|    1 | David |
|    4 | Bob   |
|    6 | Sam   |
|    7 | Mike  |
|    5 | Carol |
|    9 | John  |
+------+-------+
7 rows in set (0.00 sec)

以下是在跳过某些记录的情况下以 DESC 顺序显示记录的查询。在此处,我们在跳过 3 条记录的情况下显示了 4 条记录 -

mysql> select Name from DemoTable order by Id DESC limit 3,4;

这将产生以下输出 -

+-------+
| Name  |
+-------+
| Sam   |
| Carol |
| Bob   |
| David |
+-------+
4 rows in set (0.00 sec)

更新于:2019-09-27

68 次浏览

开启你的 事业

通过完成课程获得认证

开始
广告