如何在 MySQL 中跳过前 10 个结果?
mysqlmysqli database
要跳过前 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)
现在您可以借助 insert 命令在表中插入一些记录。查询如下 −
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;
The following is the output displays only the last 5 records since we skipped the first 10 records −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 110 | Aaron | | 111 | Peter | | 112 | Travis | | 113 | Alex | | 114 | Pat | +-----------+-------------+ 5 rows in set (0.00 sec)