鍍金池/ 教程/ 數(shù)據(jù)庫(kù)/ MariaDB Union運(yùn)算符
MariaDB比較運(yùn)算符
MariaDB查詢數(shù)據(jù)
MariaDB Like子句
MariaDB Sum()函數(shù)
MariaDB過(guò)程
MariaDB限制返回記錄
MariaDB Count()函數(shù)
MariaDB更新數(shù)據(jù)
MariaDB導(dǎo)出數(shù)據(jù)
MariaDB Intersect運(yùn)算符
MariaDB函數(shù)
MariaDB Min()函數(shù)
MariaDB創(chuàng)建數(shù)據(jù)庫(kù)
MariaDB Avg()函數(shù)
MariaDB刪除數(shù)據(jù)
MariaDB條件
MariaDB功能特點(diǎn)
MariaDB創(chuàng)建表
MariaDB左外連接
MariaDB Union運(yùn)算符
MariaDB安裝
MariaDB選擇數(shù)據(jù)庫(kù)
MariaDB Max()函數(shù)
MariaDB Where子句
MariaDB右外連接
MariaDB內(nèi)連接
MariaDB截?cái)啾?/span>
MariaDB Order By子句
MariaDB教程
MariaDB正則表達(dá)式
MariaDB Union All運(yùn)算符
MariaDB Distinct子句
MariaDB修改表
MariaDB刪除表
MariaDB插入數(shù)據(jù)
MariaDB From子句
MariaDB刪除數(shù)據(jù)庫(kù)
MariaDB簡(jiǎn)介
MariaDB數(shù)據(jù)類型

MariaDB Union運(yùn)算符

在MariaDB數(shù)據(jù)庫(kù)中,UNION運(yùn)算符用于組合兩個(gè)或更多SELECT語(yǔ)句的結(jié)果集。它刪除各種SELECT語(yǔ)句之間的重復(fù)行。

語(yǔ)法

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

注意:UNION運(yùn)算符中的每個(gè)SELECT語(yǔ)句在具有相似數(shù)據(jù)類型的結(jié)果集中必須具有相同數(shù)量的字段。

1. 使用UNION操作符返回單個(gè)字段

下面來(lái)看一個(gè)從多個(gè)SELECT語(yǔ)句返回一個(gè)字段的UNION運(yùn)算符的例子。(兩個(gè)表具有相同的公共字段)。

假設(shè)有兩張表:studentsteachers。對(duì)應(yīng)的表結(jié)構(gòu)和數(shù)據(jù)如下 -

students表中的數(shù)據(jù):

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表中的數(shù)據(jù):

USE testdb;
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 )
);
-- 插入數(shù)據(jù)
INSERT INTO teachers  
(teacher_id, name, address, admission_date)  
VALUES(1,'陳小祥','上海','2013-06-07 00:00:00');

INSERT INTO teachers  
(teacher_id, name, address, admission_date)  
VALUES(2,'張?zhí)旖?jīng)','廣州','2013-08-08 00:00:00');

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

經(jīng)過(guò)上創(chuàng)建和插入數(shù)據(jù),現(xiàn)在teachers表中擁有以下數(shù)據(jù)記錄 -

MariaDB [testdb]> select * from teachers;
+------------+--------+---------+----------------+
| teacher_id | name   | address | admission_date |
+------------+--------+---------+----------------+
|          1 | 陳小祥 | 上海    | 2013-06-07     |
|          2 | 張?zhí)旖?jīng) | 廣州    | 2013-08-08     |
|          3 | 李四光 | 三亞    | 2014-09-07     |
+------------+--------+---------+----------------+
3 rows in set (0.08 sec)

假設(shè)現(xiàn)在要查詢所有學(xué)生和老師的姓名,以及他們的地址,可參考以下查詢語(yǔ)句 -

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

執(zhí)行上面查詢語(yǔ)句,得到以下結(jié)果 -

MariaDB [testdb]> SELECT student_name AS name, student_address as address
    -> FROM Students
    -> UNION
    -> SELECT name,address
    -> FROM teachers;
+---------+-----------+
| name    | address   |
+---------+-----------+
| Maxsu   | Haikou    |
| JMaster | Beijing   |
| Mahesh  | Guangzhou |
| Kobe    | Shanghai  |
| Blaba   | Shenzhen  |
| 陳小祥  | 上海      |
| 張?zhí)旖?jīng)  | 廣州      |
| 李四光  | 三亞      |
+---------+-----------+
8 rows in set (0.05 sec)

它顯示了兩個(gè)表中并刪除重復(fù)的name列的值。

2. 具有ORDER BY子句的UNION運(yùn)算符

使用ORDER BY子句的UNION運(yùn)算符從兩個(gè)表中檢索多個(gè)列。參考以下語(yǔ)句 -

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

執(zhí)行上面示例代碼,得到以下結(jié)果 -

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

3. 一些其它的用法

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

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

指定UNION的全局順序并限制總行數(shù):

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

添加一個(gè)常數(shù)行:

(SELECT 'John Doe' AS name, 'john.doe@example.net' AS email)
UNION
(SELECT name, email FROM customers);

不同的類型:

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

按照每個(gè)SELECT使用排序列的順序返回結(jié)果:

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