MariaDB Union All運算符

MariaDB UNION ALL操作符與UNION操作符相同,但不會刪除重複的記錄。它返回查詢中的所有行,並且不刪除各種SELECT語句之間的重複行。

語法

SELECT expression1, expression2, ... expression_n  
FROM tables  
[WHERE conditions]  
UNION ALL  
SELECT expression1, expression2, ... expression_n  
FROM tables  
[WHERE conditions];

注意:UNION All運算符中的每個SELECT語句在具有相似數據類型的結果集中必須具有相同數量的字段。

1. 使用UNION ALL操作符返回單個字段

下面來看一個從多個SELECT語句返回一個字段的UNION運算符的例子。(兩個表具有相同的公共字段)。

假設有兩張表:studentsteachers。對應的表結構和數據如下 -

students表中的數據:

MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          2 | JMaster      | Beijing         | 2016-05-07     |
|          3 | Mahesh       | Guangzhou       | 2016-06-07     |
|          4 | Kobe         | Shanghai        | 2016-02-07     |
|          5 | Blaba        | Shenzhen        | 2016-08-07     |
+------------+--------------+-----------------+----------------+
5 rows in set (0.00 sec)

teachers表中的數據:

USE testdb;
DROP TABLE teachers;
CREATE TABLE teachers(  
    teacher_id INT NOT NULL AUTO_INCREMENT,  
    name VARCHAR(100) NOT NULL,  
    address VARCHAR(40) NOT NULL,  
    admission_date DATE,  
    PRIMARY KEY ( teacher_id )
);
-- 插入數據
INSERT INTO teachers  
(teacher_id, name, address, admission_date)  
VALUES(1,'Maxsu','Haikou','2013-06-07 00:00:00');

INSERT INTO teachers  
(teacher_id, name, address, admission_date)  
VALUES(2,'張天經','廣州','2013-08-08 00:00:00');

INSERT INTO teachers  
(teacher_id, name, address, admission_date)  
VALUES(3,'李四光','三亞','2014-09-07 00:00:00');

經過上創建和插入數據,現在teachers表中擁有以下數據記錄 -

MariaDB [testdb]> select * from teachers;
+------------+--------+---------+----------------+
| teacher_id | name   | address | admission_date |
+------------+--------+---------+----------------+
|          1 | Maxsu  | Haikou  | 2013-06-07     |
|          2 | 張天經 | 廣州    | 2013-08-08     |
|          3 | 李四光 | 三亞    | 2014-09-07     |
+------------+--------+---------+----------------+
3 rows in set (0.00 sec)

假設現在要查詢所有學生和老師的姓名,以及他們的地址,可參考以下查詢語句 -

SELECT student_name AS name, student_address as address
FROM Students
UNION ALL
SELECT name,address
FROM teachers;

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> SELECT student_name AS name, student_address as address
    -> FROM Students
    -> UNION ALL
    -> SELECT name,address
    -> FROM teachers;
+---------+-----------+
| name    | address   |
+---------+-----------+
| Maxsu   | Haikou    |
| JMaster | Beijing   |
| Mahesh  | Guangzhou |
| Kobe    | Shanghai  |
| Blaba   | Shenzhen  |
| Maxsu   | Haikou    |
| 張天經  | 廣州      |
| 李四光  | 三亞      |
+---------+-----------+
8 rows in set (0.00 sec)

注:如果這裏不使用UNION ALL,而是使用UNION,那麼查詢結果是什麼?

它顯示了兩個表中重複的name列的值 - 'Maxsu'

2. 具有ORDER BY子句的UNION運算符

使用ORDER BY子句的UNION運算符從兩個表中檢索多個列。參考以下語句 -

SELECT student_id, student_name  
FROM Students  
WHERE student_name = 'Maxsu'  
UNION  ALL
SELECT teacher_id, address
FROM teachers  
WHERE teacher_id < 3 
ORDER BY 1;

執行上面示例代碼,得到以下結果 -

MariaDB [testdb]> SELECT student_id, student_name
    -> FROM Students
    -> WHERE student_name = 'Maxsu'
    -> UNION  ALL
    -> SELECT teacher_id, address
    -> FROM teachers
    -> WHERE teacher_id < 3
    -> ORDER BY 1;
+------------+--------------+
| student_id | student_name |
+------------+--------------+
|          1 | Haikou       |
|          1 | Maxsu        |
|          2 | 廣州         |
+------------+--------------+
3 rows in set (0.00 sec)

3. 一些其它的用法

具有不同列名的表之間的UNION:

(SELECT e_name AS name, email FROM employees)
UNION ALL
(SELECT c_name AS name, email FROM customers);

指定UNION的全局順序並限制總行數:

(SELECT name, email FROM employees)
UNION ALL
(SELECT name, email FROM customers)
ORDER BY name LIMIT 10;

添加一個常數行:

(SELECT 'John Doe' AS name, '[email protected]' AS email)
UNION ALL
(SELECT name, email FROM customers);

不同的類型:

SELECT CAST('x' AS CHAR(1)) UNION ALL SELECT REPEAT('y',4);
+----------------------+
| CAST('x' AS CHAR(1)) |
+----------------------+
| x                    |
| yyyy                 |
+----------------------+

按照每個SELECT使用排序列的順序返回結果:

(SELECT 1 AS sort_column, e_name AS name, email FROM employees)
UNION ALL
(SELECT 2, c_name AS name, email FROM customers) ORDER BY sort_column;