MySQL 查询按 DESC 排序显示记录并跳过一些记录?

mysqlmysqli database更新于 2025/12/24 2:52:17

首先我们创建一个表 −

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)

以下查询以降序显示记录,同时跳过一些记录。这里,我们显示 4 条记录,同时跳过 3 条 −

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

这将产生以下输出 −

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

相关文章


有用资源