SQL UNION 语句

SQL UNION 语句/运算符用于组合两个或多个 SELECT 语句的结果而不返回任何重复的行。

要使用此 UNION 语句,每个 SELECT 语句必须具有以下规则

  • 选择的列数相同
  • 列数相同的表达式
  • 相同的数据类型
  • 让它们按相同的顺序

但它们不必具有相同的长度。


语法

UNION子句的基本语法如下 -

SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]

UNION

SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]

在这里,给定的条件可以是基于我们要求的任何给定的表达式。

示例

现在我们看如下的两张表

1. CUSTOMERS 表

+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

2. ORDERS 表

+-----+---------------------+-------------+--------+
| OID | DATE                | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 100 | 2009-10-08 00:00:00 |           3 |   1500 |
| 101 | 2009-11-20 00:00:00 |           3 |   1560 |
| 102 | 2009-10-08 00:00:00 |           3 |   3000 |
| 103 | 2008-05-20 00:00:00 |           4 |   2060 |
| 104 | 2008-05-21 00:00:00 |           9 |   2160 |
+-----+---------------------+-------------+--------+

现在,让我们在 SELECT 语句中查询这两个表,如下所示

SQL> SELECT  ID, NAME, AMOUNT, DATE
   FROM CUSTOMERS
   LEFT JOIN ORDERS
   ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
UNION
   SELECT  ID, NAME, AMOUNT, DATE
   FROM CUSTOMERS
   RIGHT JOIN ORDERS
   ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

结果如下:

+------+----------+--------+---------------------+
| ID   | NAME     | AMOUNT | DATE                |
+------+----------+--------+---------------------+
|    3 | kaushik  |   1500 | 2009-10-08 00:00:00 |
|    3 | kaushik  |   1560 | 2009-11-20 00:00:00 |
|    3 | kaushik  |   3000 | 2009-10-08 00:00:00 |
|    4 | Chaitali |   2060 | 2008-05-20 00:00:00 |
|    1 | Ramesh   |   NULL | NULL                |
|    2 | Khilan   |   NULL | NULL                |
|    5 | Hardik   |   NULL | NULL                |
|    6 | Komal    |   NULL | NULL                |
|    7 | Muffy    |   NULL | NULL                |
| NULL | NULL     |   2160 | 2008-05-21 00:00:00 |
+------+----------+--------+---------------------+

UNION ALL

UNION ALL 运算符用于组合包括重复行在内的两个 SELECT 语句的结果。

和 UNION 语句具有相同的规则。

语法

UNION ALL的基本语法如下。

SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]

UNION ALL

SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]

示例

现在,让我们使用 SELECT 语句查询上例中的两个表,如下所示

SQL> SELECT  ID, NAME, AMOUNT, DATE
   FROM CUSTOMERS
   LEFT JOIN ORDERS
   ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
UNION ALL
   SELECT  ID, NAME, AMOUNT, DATE
   FROM CUSTOMERS
   RIGHT JOIN ORDERS
   ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

结果如下:

+------+----------+--------+---------------------+
| ID   | NAME     | AMOUNT | DATE                |
+------+----------+--------+---------------------+
|    3 | kaushik  |   1500 | 2009-10-08 00:00:00 |
|    3 | kaushik  |   1560 | 2009-11-20 00:00:00 |
|    3 | kaushik  |   3000 | 2009-10-08 00:00:00 |
|    4 | Chaitali |   2060 | 2008-05-20 00:00:00 |
|    1 | Ramesh   |   NULL | NULL                |
|    2 | Khilan   |   NULL | NULL                |
|    5 | Hardik   |   NULL | NULL                |
|    6 | Komal    |   NULL | NULL                |
|    7 | Muffy    |   NULL | NULL                |
|    3 | kaushik  |   1500 | 2009-10-08 00:00:00 |
|    3 | kaushik  |   1560 | 2009-11-20 00:00:00 |
|    3 | kaushik  |   3000 | 2009-10-08 00:00:00 |
|    4 | Chaitali |   2060 | 2008-05-20 00:00:00 |
| NULL | NULL     |   2160 | 2008-05-21 00:00:00 |
+------+----------+--------+---------------------+

还有另外两个语句(即运算符),它们的功能类似于 UNION 语句。

  • SQL INTERSECT 子句- 这用于组合两个 SELECT 语句,但仅从第一个 SELECT 语句中返回与第二个 SELECT 语句中的行相同的行。
  • SQL EXCEPT 子句- 这结合了两个 SELECT 语句,并从第一个 SELECT 语句返回第二个 SELECT 语句未返回的行。

查看笔记

扫码一下
查看教程更方便