如何在 MySQL 中跳过前 10 个结果?


若要跳过前 10 个结果,请使用“limit offset”。语法如下 −

select *from yourTableName limit 10 offset lastValue;

让我们创建一个表来理解上述语法。以下是创建表的查询 −

mysql> create table SkipFirstTenRecords
   −> (
      −> StudentId int,
      −> StudentName varchar(200)
   −> );
Query OK, 0 rows affected (0.53 sec)

现在,你可以使用插入命令插入一些记录到表中。查询如下 −

mysql> insert into SkipFirstTenRecords values(100,'John');
Query OK, 1 row affected (0.12 sec)

mysql> insert into SkipFirstTenRecords values(101,'Johnson');
Query OK, 1 row affected (0.14 sec)

mysql> insert into SkipFirstTenRecords values(102,'Carol');
Query OK, 1 row affected (0.10 sec)

mysql> insert into SkipFirstTenRecords values(103,'Smith');
Query OK, 1 row affected (0.32 sec)

mysql> insert into SkipFirstTenRecords values(104,'Bob');
Query OK, 1 row affected (0.14 sec)

mysql> insert into SkipFirstTenRecords values(105,'David');
Query OK, 1 row affected (0.18 sec)

mysql> insert into SkipFirstTenRecords values(106,'Sam');
Query OK, 1 row affected (0.14 sec)

mysql> insert into SkipFirstTenRecords values(107,'Taylor');
Query OK, 1 row affected (0.23 sec)

mysql> insert into SkipFirstTenRecords values(108,'Ramit');
Query OK, 1 row affected (0.16 sec)

mysql> insert into SkipFirstTenRecords values(109,'Belly');
Query OK, 1 row affected (0.18 sec)

mysql> insert into SkipFirstTenRecords values(110,'Aaron ');
Query OK, 1 row affected (0.16 sec)

mysql> insert into SkipFirstTenRecords values(111,'Peter');
Query OK, 1 row affected (0.10 sec)

mysql> insert into SkipFirstTenRecords values(112,'Travis');
Query OK, 1 row affected (0.14 sec)

mysql> insert into SkipFirstTenRecords values(113,'Alex');
Query OK, 1 row affected (0.18 sec)

mysql> insert into SkipFirstTenRecords values(114,'Pat ');
Query OK, 1 row affected (0.11 sec)

Display all records which I have inserted in the table. The query is as follows:

mysql> select *from SkipFirstTenRecords;

以下是输出 −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
|       100 | John        |
|       101 | Johnson     |
|       102 | Carol       |
|       103 | Smith       |
|       104 | Bob         |
|       105 | David       |
|       106 | Sam         |
|       107 | Taylor      |
|       108 | Ramit       |
|       109 | Belly       |
|       110 | Aaron       |
|       111 | Peter       |
|       112 | Travis      |
|       113 | Alex        |
|       114 | Pat         |
+-----------+-------------+
15 rows in set (0.00 sec)

从上述表中跳过前 10 个记录的查询如下 −

mysql> select *from SkipFirstTenRecords limit 10 offset 10;

以下输出仅显示最后 5 个记录,因为我们跳过了前 10 个记录 −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
|        110 | Aaron      |
|        111 | Peter      |
|        112 | Travis     |
|        113 | Alex       |
|        114 | Pat        |
+-----------+-------------+
5 rows in set (0.00 sec)

更新于: 2020-06-25

1 千+ 浏览

开启您的 职业

通过完成课程获取认证

开始
广告
© . All rights reserved.