標籤:style blog color ar 資料 div sp log on
MySQL支援limit和offset的文法。本文主要用於備忘
閑話少敘,我們建立一張實驗用的表:
mysql> create table limit_offset( -> id int auto_increment primary key, -> name varchar(20) -> );Query OK, 0 rows affected (0.01 sec)
插入資料:insert into limit_offset(name) value(val),我們插入20條資料
mysql> select * from limit_offset;+----+--------+| id | name |+----+--------+| 1 | name1 || 2 | name2 || 3 | name3 || 4 | name4 || 5 | name5 || 6 | name6 || 7 | name7 || 8 | name8 || 9 | name9 || 10 | name10 || 11 | name11 || 12 | name12 || 13 | name13 || 14 | name14 || 15 | name15 || 16 | name16 || 17 | name17 || 18 | name18 || 19 | name19 || 20 | name20 |+----+--------+20 rows in set (0.00 sec)
用法一:limit m。該文法是選擇ResultSet中的前m條,包括m
mysql> select * from limit_offset limit 6;+----+-------+| id | name |+----+-------+| 1 | name1 || 2 | name2 || 3 | name3 || 4 | name4 || 5 | name5 || 6 | name6 |+----+-------+6 rows in set (0.00 sec)
用法二:limit m,n。該文法是選擇從ResultSet中的第m條記錄開始向後選擇n條記錄,不包括m
mysql> select * from limit_offset limit 10,6;+----+--------+| id | name |+----+--------+| 11 | name11 || 12 | name12 || 13 | name13 || 14 | name14 || 15 | name15 || 16 | name16 |+----+--------+6 rows in set (0.00 sec)
用法三:limit m offset n。表示從第n條記錄開始向後選擇m條記錄,不包括n(跟limit n,m)等價
mysql> select * from limit_offset limit 6 offset 10;+----+--------+| id | name |+----+--------+| 11 | name11 || 12 | name12 || 13 | name13 || 14 | name14 || 15 | name15 || 16 | name16 |+----+--------+6 rows in set (0.00 sec)
MySQL limit offset文法