千家信息网

MySQL 5.7 LIMIT语句介绍

发表于:2025-01-21 作者:千家信息网编辑
千家信息网最后更新 2025年01月21日,LIMIT语句可以限制SELECT语句返回的行数。LIMIT使用1个或2个参数,参数必须是非负数的整数。mysql> select * from dept2 order by 1;+--------+
千家信息网最后更新 2025年01月21日MySQL 5.7 LIMIT语句介绍LIMIT语句可以限制SELECT语句返回的行数。LIMIT使用1个或2个参数,参数必须是非负数的整数。

mysql> select * from dept2 order by 1;
+--------+-------+
| deptno | dname |
+--------+-------+
| 10 | A |
| 20 | B |
| 30 | C |
| 40 | D |
| 50 | E |
| 60 | F |
| 70 | G |
| 80 | H |
| 90 | I |
| 100 | J |
| 110 | K |
| 120 | L |
| 130 | M |
| 140 | N |
| 150 | O |
| 160 | P |
| 170 | Q |
| 180 | R |
| 190 | S |
| 200 | T |
+--------+-------+
20 rows in set (0.03 sec)

--返回第6-15行

mysql> select * from dept2 order by 1 limit 5,10;
+--------+-------+
| deptno | dname |
+--------+-------+
| 60 | F |
| 70 | G |
| 80 | H |
| 90 | I |
| 100 | J |
| 110 | K |
| 120 | L |
| 130 | M |
| 140 | N |
| 150 | O |
+--------+-------+
10 rows in set (0.00 sec)

--返回前5行

mysql> select * from dept2 order by 1 limit 5;
+--------+-------+
| deptno | dname |
+--------+-------+
| 10 | A |
| 20 | B |
| 30 | C |
| 40 | D |
| 50 | E |
+--------+-------+
5 rows in set (0.00 sec)

mysql> select * from dept2 order by 1 limit 0,5;
+--------+-------+
| deptno | dname |
+--------+-------+
| 10 | A |
| 20 | B |
| 30 | C |
| 40 | D |
| 50 | E |
+--------+-------+
5 rows in set (0.00 sec)
0