---
title: 编组
description: "在查询中使用 LIMIT LIMIT 可以限制 SELECT 查询返回的行数，常用于分页操作。 LIMIT 语法 查询中包含 LIMIT 的 SQL 语句格式如下： SELECT select_list FROM table_list LIMIT [offset,] count_num; 参数说明如下： offset…"
---
切换语言

- 中文站 - 简体中文
- International - English
- 日本站 - 日本語

文档反馈![](https://mdn.alipayobjects.com/huamei_22khvb/afts/img/A*P8CuR4UJ_FkAAAAAAAAAAAAADiGDAQ/original) OceanBase 数据库分布式版 - V 4.0.0 企业版

# 在查询中使用 LIMIT

更新时间：2026-05-25 20:32:12

`LIMIT` 可以限制 `SELECT` 查询返回的行数，常用于分页操作。

## LIMIT 语法

查询中包含 `LIMIT` 的 SQL 语句格式如下：

```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 行记录

```sql
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

```

### 获取最大的一条记录

如果想获取订单金额最大的一条记录，可以先按照金额降序，然后取第一条记录。

```sql
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` 时，查询结果会返回所有的剩余数据。

```sql
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 时，查询结果取所有剩余数据

```sql
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 语句格式如下：

```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 页的数据。

```sql
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` 这些关键字一起使用时，请遵照如下语法中指定的先后顺序：

```sql
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;

```

示例如下：

```sql
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` 中不能使用表达式，只能跟随明确的数字。

  ```sql
  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 1

  ```
 - `LIMIT` 后面的数字不能为负数。

  ```sql
  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

  ```

 上一篇 下一篇 ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ)[AI](https://www.oceanbase.com/obi) 咨询热线
