---
title: "UNION 子句 - OceanBase 数据库 V4.5.0 | OceanBase 文档中心"
description: UNION 子句 描述 该语句用于对多个 SELECT 查询的结果进行集合操作。 使用限制及注意事项 集合运算符具有相同的优先级。如果 SQL 语句包含多个集合运算符，则 OceanBase 数据库将按照从左到右的顺序对它们进行计算，除非使用括号明确指定了运算的顺序。 各个集合运算符只能操作 SELECT 语句的结果…
---
切换语言

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

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

# UNION 子句

更新时间：2026-04-08 15:23:25

[编辑](https://github.com/oceanbase/oceanbase-doc/edit/V4.5.0/zh-CN/700.reference/500.sql-reference/100.sql-syntax/200.common-tenant-of-mysql-mode/600.sql-statement-of-mysql-mode/8100.select-of-mysql-mode/400.union-clause-of-mysql-mode.md)  

## 描述

该语句用于对多个 `SELECT` 查询的结果进行集合操作。

## 使用限制及注意事项

- 集合运算符具有相同的优先级。如果 SQL 语句包含多个集合运算符，则 OceanBase 数据库将按照从左到右的顺序对它们进行计算，除非使用括号明确指定了运算的顺序。
 - 各个集合运算符只能操作 `SELECT` 语句的结果集，且各个结果集的列数和数据类型必须相同。

## 语法

```sql
select_stmt
    {UNION | UNION ALL | MINUS | EXCEPT | INTERSECT} select_stmt
    [, {UNION | UNION ALL | MINUS | EXCEPT | INTERSECT} select_stmt ...]
    [ORDER BY order_by_condition_list]
    [LIMIT limit_clause];

```

## 参数解释

| 参数 | 描述 |
| --- | --- |
| select_stmt | 用于集合计算的 `SELECT` 语句。有关 `SELECT` 语句的介绍信息，参见 [SELECT](https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000004480787)。 |
| UNION | 用于将两个 `SELECT` 语句的结果集合并为一个集合，并去除重复的行。 |
| UNION ALL | 用于将两个 `SELECT` 语句的结果集合并为一个集合，不去除重复的行。 |
| MINUS \| EXCEPT | 用于返回前一个 `SELECT` 语句的结果集中不包含在后一个 `SELECT` 语句的结果集中的行。`MINUS` 是 `EXCEPT` 的同义词。 |
| INTERSECT | 用于返回两个 `SELECT` 语句的结果集的交集，并去重。 |
| ORDER BY order_by_condition_list | 用于指定集合计算后进行排序。 |
| LIMIT limit_clause | 用于指定集合计算后返回的行数。 |

## 示例

1. 创建表 `test_tbl1` 和 `test_tbl2`。

   ```sql
   CREATE TABLE test_tbl1 (col1 INT, col2 INT);
   CREATE TABLE test_tbl2 (col1 INT, col2 INT);

   ```
 2. 向表 `test_tbl1` 和 `test_tbl2` 中插入测试数据。

   ```sql
   INSERT INTO test_tbl1 VALUES (1, 1), (2, 2), (4, 4);
   INSERT INTO test_tbl2 VALUES (2, 2), (3, 3), (5, 5);

   ```

- 从表 `test_tbl1` 和 `test_tbl2` 中选择 `col1` 和 `col2` 列的数据，并使用 `UNION ALL` 操作符将它们合并到一个结果集中，将保留重复的行。

  ```sql
  SELECT col1, col2 FROM test_tbl1
  UNION ALL
  SELECT col1, col2 FROM test_tbl2;

  ```

  返回结果如下：

  ```shell
  +------+------+
  | col1 | col2 |
  +------+------+
  |    1 |    1 |
  |    2 |    2 |
  |    4 |    4 |
  |    2 |    2 |
  |    3 |    3 |
  |    5 |    5 |
  +------+------+
  6 rows in set

  ```
 - 从表 `test_tbl1` 和 `test_tbl2` 中选择 `col1` 和 `col2` 列的数据，并使用 `UNION` 操作符将它们合并到一个结果集中，将去除重复的行。

  ```sql
  SELECT col1, col2 FROM test_tbl1
  UNION
  SELECT col1, col2 FROM test_tbl2;

  ```

  返回结果如下：

  ```shell
  +------+------+
  | col1 | col2 |
  +------+------+
  |    1 |    1 |
  |    2 |    2 |
  |    4 |    4 |
  |    3 |    3 |
  |    5 |    5 |
  +------+------+
  5 rows in set

  ```
 - 从 `test_tbl1` 表和 `test_tbl2` 表中选择 `col1` 和 `col2` 列的数据，并返回两个表的交集。即返回 `test_tbl1` 和 `test_tbl2` 中 `col1` 和 `col2` 列的数据中重复的部分。

  ```sql
  SELECT col1, col2 FROM test_tbl1
  INTERSECT
  SELECT col1, col2 FROM test_tbl2;

  ```

  返回结果如下：

  ```shell
  +------+------+
  | col1 | col2 |
  +------+------+
  |    2 |    2 |
  +------+------+
  1 row in set

  ```
 - 从 `test_tbl1` 表中选择 `col1` 和 `col2` 列的数据，然后排除 `test_tbl2` 表中相同的 `col1` 和 `col2` 列的数据。即返回 `test_tbl1` 中有而 `test_tb2` 中没有的数据行。

  ```sql
  SELECT col1, col2 FROM test_tbl1
  EXCEPT
  SELECT col1, col2 FROM test_tbl2;

  ```

  返回结果如下：

  ```shell
  +------+------+
  | col1 | col2 |
  +------+------+
  |    1 |    1 |
  |    4 |    4 |
  +------+------+
  2 rows in set

  ```
 - 从 `test_tbl1` 和 `test_tbl2` 中获取 `col1` 和 `col2` 的数据，并将它们合并在一起。然后对合并后的结果按 `col1` 降序进行排序，并且只返回前 3 个结果。

  ```sql
  SELECT col1, col2 FROM test_tbl1
  UNION
  SELECT col1, col2 FROM test_tbl2
  ORDER BY col1 DESC
  LIMIT 3;

  ```

  返回结果如下：

  ```shell
  +------+------+
  | col1 | col2 |
  +------+------+
  |    5 |    5 |
  |    4 |    4 |
  |    3 |    3 |
  +------+------+
  3 rows in set

  ```

## 相关文档

- [SELECT](https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000004480787)
 - [集合操作](https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000004476099)

 上一篇 下一篇 ![有帮助](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) 咨询热线
