LIMIT 可以限制 SELECT 查询返回的行数,常用于分页操作。
LIMIT 语法
查询中包含 LIMIT 的 SQL 语句格式如下:
SELECT select_list FROM table_list LIMIT [offset,] count_num;
参数说明如下:
offset表示偏移量,即跳过多少行。offset可以省略,默认为 0,表示跳过 0 行;offset取值范围为 [0,+∞)。count_num表示跳过offset行之后开始获取数据,取count_num行记录;count_num取值范围为 [0,+∞)。offset和count_num的值不能使用表达式。
使用 LIMIT n, m 查询示例
获取前 m 行记录
obclient> SELECT order_id ID, fruit_price 订单金额 FROM fruit_order LIMIT 5;
+----+--------------+
| ID | 订单金额 |
+----+--------------+
| 1 | 13.11 |
| 2 | 23.34 |
| 3 | 12.22 |
| 4 | 22.21 |
| 5 | 51.55 |
+----+--------------+
5 rows in set
obclient> SELECT order_id ID, fruit_price 订单金额 FROM fruit_order LIMIT 0,5;
+----+--------------+
| ID | 订单金额 |
+----+--------------+
| 1 | 13.11 |
| 2 | 23.34 |
| 3 | 12.22 |
| 4 | 22.21 |
| 5 | 51.55 |
+----+--------------+
5 rows in set
获取最大的一条记录
如果想获取订单金额最大的一条记录,可以先按照金额降序,然后取第一条记录。
obclient> SELECT order_id ID, fruit_price 订单金额 FROM fruit_order ORDER BY fruit_price DESC LIMIT 1;
+----+--------------+
| ID | 订单金额 |
+----+--------------+
| 9 | 63.66 |
+----+--------------+
1 row in set
可以看到,当跳过 n 行记录、剩余行数小于 m 时,查询结果会返回所有的剩余数据。
obclient> SELECT order_id ID, fruit_price 订单金额 FROM fruit_order LIMIT 3,7;
+----+--------------+
| ID | 订单金额 |
+----+--------------+
| 4 | 22.21 |
| 5 | 51.55 |
| 6 | 58.83 |
| 7 | 14.66 |
| 8 | 34.44 |
| 9 | 63.66 |
+----+--------------+
6 rows in set
跳过 n 行记录后,剩余数据的行数小于 m 时,查询结果取所有剩余数据
obclient> SELECT order_id ID, fruit_price 订单金额 FROM fruit_order;
+----+--------------+
| ID | 订单金额 |
+----+--------------+
| 1 | 13.11 |
| 2 | 23.34 |
| 3 | 12.22 |
| 4 | 22.21 |
| 5 | 51.55 |
| 6 | 58.83 |
| 7 | 14.66 |
| 8 | 34.44 |
| 9 | 63.66 |
+----+--------------+
9 rows in set
obclient> SELECT order_id ID, fruit_price 订单金额 FROM fruit_order LIMIT 3,7;
+----+--------------+
| ID | 订单金额 |
+----+--------------+
| 4 | 22.21 |
| 5 | 51.55 |
| 6 | 58.83 |
| 7 | 14.66 |
| 8 | 34.44 |
| 9 | 63.66 |
+----+--------------+
6 rows in set
分页查询
LIMIT 分页查询的 SQL 语句格式如下:
SELECT select_list FROM table_list LIMIT (page_no - 1) * page_size, page_size;
语法的参数说明如下:
page_no表示第几页,从 1 开始,范围为 [1,+∞)。page_size表示每页显示多少条记录,范围为 [1,+∞)。例如:page_no = 5,page_size = 10,表示获取第 5 页 10 条数据。
如下示例为,每页显示 2 条数据,依次获取第 1 页、第 2 页和第 3 页的数据。
obclient> SELECT order_id ID, fruit_price 订单金额 FROM fruit_order ORDER BY fruit_price DESC;
+----+--------------+
| ID | 订单金额 |
+----+--------------+
| 9 | 63.66 |
| 6 | 58.83 |
| 5 | 51.55 |
| 8 | 34.44 |
| 2 | 23.34 |
| 4 | 22.21 |
| 7 | 14.66 |
| 1 | 13.11 |
| 3 | 12.22 |
+----+--------------+
9 rows in set
obclient> SELECT order_id ID, fruit_price 订单金额 FROM fruit_order ORDER BY fruit_price DESC LIMIT 0,2;
+----+--------------+
| ID | 订单金额 |
+----+--------------+
| 9 | 63.66 |
| 6 | 58.83 |
+----+--------------+
2 rows in set
obclient> SELECT order_id ID, fruit_price 订单金额 FROM fruit_order ORDER BY fruit_price DESC LIMIT 2,2;
+----+--------------+
| ID | 订单金额 |
+----+--------------+
| 5 | 51.55 |
| 8 | 34.44 |
+----+--------------+
2 rows in set
obclient> SELECT order_id ID, fruit_price 订单金额 FROM fruit_order ORDER BY fruit_price DESC LIMIT 4,2;
+----+--------------+
| ID | 订单金额 |
+----+--------------+
| 2 | 23.34 |
| 4 | 22.21 |
+----+--------------+
2 rows in set
WHERE & GROUP BY & HAVING & ORDER BY & LIMIT 协作
当 WHERE、GROUP BY、HAVING、ORDER BY、LIMIT 这些关键字一起使用时,请遵照如下语法中指定的先后顺序:
SELECT select_list FROM table_name
WHERE query_condition
GROUP BY group_by_expression
HAVING group_condition
ORDER BY column_list][ASC | DESC]
LIMIT [offset,] count_num;
示例如下:
obclient> SELECT user_id 客户ID, COUNT(order_id) 下单数量 FROM fruit_order t
WHERE t.order_year = 2021
GROUP BY user_id
HAVING count(order_id)>=2
ORDER BY 下单数量 DESC
LIMIT 2;
+----------+--------------+
| 客户ID | 下单数量 |
+----------+--------------+
| 1022 | 2 |
+----------+--------------+
1 row in set
注意事项
LIMIT中不能使用表达式,只能跟随明确的数字。obclient> SELECT order_id ID, fruit_price 订单金额 FROM fruit_order LIMIT (3+1),7; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your OceanBase version for the right syntax to use near '(3+1),7' at line 1LIMIT后面的数字不能为负数。obclient> SELECT order_id ID, fruit_price 订单金额 FROM fruit_order LIMIT -3; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your OceanBase version for the right syntax to use near '-3' at line 1
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




