使用查詢語句的時候,經常要返回前幾條或者中間某幾行資料,這個時候怎麼辦呢?不用擔心,
mysql已 經為我們提供了這樣一個功能。
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
LIMIT 子句可以被用於強制 SELECT 語句返回指定的記錄數。LIMIT 接受一個或兩個數字參數。參數必須是一個整數常量。如果給定兩個參數,第一個參數指定第一個返回記錄行的位移量,第二個參數指定返回記錄行的最大數目。初 始記錄行的位移量是 0(而不是 1): 為了與 PostgreSQL 相容,MySQL 也支援句法: LIMIT # OFFSET #。
mysql> SELECT * FROM table LIMIT 5,10; // 檢索記錄行 6-15
//為了檢索從某一個位移量到記錄集的結束所有的記錄行,可以指定第二個參數為 -1:
mysql> SELECT * FROM table LIMIT 95,-1; // 檢索記錄行 96-last.
//如果只給定一個參數,它表示返回最大的記錄行數目:
mysql> SELECT * FROM table LIMIT 5; //檢索前 5 個記錄行
//換句話說,LIMIT n 等價於 LIMIT 0,n。
執行個體:
mysql> use sppeivan;
Database changed
mysql> select * from employee; // 返回所有結果
+------------+------------+--------+-------------+
| username | password | gender | cellphone |
+------------+------------+--------+-------------+
| ewrew | erewr | MAN | 13618889076 |
| ewrwerw32r | 324324 | MAN | 13618889076 |
| grrrrr | r44343 | WOMAN | 13618889076 |
| pansong | erewrwe | MAN | 13618889076 |
| ret43t43t | 43t43 | MAN | 13618889076 |
| ret44t4 | r4tgr | WOMAN | 13618889076 |
| tttt | 4et | WOMAN | 13618889076 |
| wr34r | retre | MAN | 13618889076 |
| xxx | safe | MAN | 13618889076 |
| hhh | ewrwer | MAN | 13618889076 |
| hg | 6574326543 | MAN | 13618889076 |
| ps | ewrwe | MAN | 13618889076 |
| zhu | werew | MAN | 13618889076 |
+------------+------------+--------+-------------+
13 rows in set (0.01 sec)
mysql> select * from employee limit 3 7;
ERROR 1064 (42000): You have an error in your SQL syntax
corresponds to your MySQL server version for the right s
line 1
mysql> select * from employee limit 3, 7; // 返回4-11行
+-----------+----------+--------+-------------+
| username | password | gender | cellphone |
+-----------+----------+--------+-------------+
| pansong | erewrwe | MAN | 13618889076 |
| ret43t43t | 43t43 | MAN | 13618889076 |
| ret44t4 | r4tgr | WOMAN | 13618889076 |
| tttt | 4et | WOMAN | 13618889076 |
| wr34r | retre | MAN | 13618889076 |
| xxx | safe | MAN | 13618889076 |
| hhh | ewrwer | MAN | 13618889076 |
+-----------+----------+--------+-------------+
7 rows in set (0.00 sec)
mysql> select * from employee limit 3,-1; // 錯誤文法
ERROR 1064 (42000): You have an error in your SQL syntax
corresponds to your MySQL server version for the right s
t line 1
mysql> select * from employee limit 3,1; // 返回第4行
+----------+----------+--------+-------------+
| username | password | gender | cellphone |
+----------+----------+--------+-------------+
| pansong | erewrwe | MAN | 13618889076|
+----------+----------+--------+-------------+
1 row in set (0.00 sec)
mysql> select * from employee limit 3; // 返回前3行
+------------+----------+--------+-------------+
| username | password | gender | cellphone |
+------------+----------+--------+-------------+
| ewrew | erewr | MAN | 13618889076 |
| ewrwerw32r | 324324 | MAN | 13618889076 |
| grrrrr | r44343 | WOMAN | 13618889076 |
+------------+----------+--------+-------------+
3 rows in set (0.00 sec)
mysql> select * from employee limit 10; // 返回前10行
+------------+----------+--------+-------------+
| username | password | gender | cellphone |
+------------+----------+--------+-------------+
| ewrew | erewr | MAN | 13618889076 |
| ewrwerw32r | 324324 | MAN | 13618889076 |
| grrrrr | r44343 | WOMAN | 13618889076 |
| pansong | erewrwe | MAN | 13618889076 |
| ret43t43t | 43t43 | MAN | 13618889076 |
| ret44t4 | r4tgr | WOMAN | 13618889076 |
| tttt | 4et | WOMAN | 13618889076 |
| wr34r | retre | MAN | 13618889076 |
| xxx | safe | MAN | 13811588469 |
| hhh | ewrwer | MAN | 13618889076 |
+------------+----------+--------+-------------+
10 rows in set (0.00 sec)
mysql> select * from employee limit 9,1; // 返回第10行
+----------+----------+--------+-------------+
| username | password | gender | cellphone |
+----------+----------+--------+-------------+
| hhh | ewrwer | MAN | 13816668468 |
+----------+----------+--------+-------------+
1 row in set (0.00 sec)