MariaDB中Union All的作用是什么,用法有哪些

Admin 2022-06-30 群英技术资讯 329 次浏览

今天就跟大家聊聊有关“MariaDB中Union All的作用是什么,用法有哪些”的内容,可能很多人都不太了解,为了让大家认识和更进一步的了解,小编给大家总结了以下内容,希望这篇“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, 'john.doe@example.net' 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;

到此这篇关于“MariaDB中Union All的作用是什么,用法有哪些”的文章就介绍到这了,更多相关内容请搜索群英网络以前的文章或继续浏览下面的相关文章,希望大家以后多多支持群英网络!
群英智防CDN,智能加速解决方案
标签: Union All运算符

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

猜你喜欢

成为群英会员,开启智能安全云计算之旅

立即注册
专业资深工程师驻守
7X24小时快速响应
一站式无忧技术支持
免费备案服务
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
在线客服
微信公众号
返回顶部
返回顶部 返回顶部
在线客服
在线客服