在单个查询中同时实现 MySQL 的 LIMIT 和 OFFSET,并说明它们的区别
LIMIT 告诉你希望获取多少条记录,而 OFFSET 提供给定位置 +1 开始的记录。我们先创建一个表 -
mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (1.33 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.59 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.60 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
输出
将产生如下输出 -
+--------+ | Name | +--------+ | John | | Chris | | David | | Bob | | Sam | | Mike | | Carol | | Robert | +--------+ 8 rows in set (0.00 sec)
以下是实现 MySQL 限制偏移量的查询 -
mysql> select *from DemoTable limit 4 offset 3;
输出
将产生如下输出 -
+-------+ | Name | +-------+ | Bob | | Sam | | Mike | | Carol | +-------+ 4 rows in set (0.00 sec)
广告