mysql union与union all介绍
union和union all的作用很简单,都是用来合并两条或多条sql的结果集,只是union会去掉重复的行,而union all不会合并相同的行。
在使用union和union all合并查询结果集时,需要注意以下几点:
- UNION 内部的SELECT语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条SELECT语句中的列的顺序必须相同
- 如果多个select查询结果的列名称不相同,则UNION结果集中的列名总是等于第一个SELECT 语句中的列名
- 使用UNION进行联合查询,如果不同的select语句中取出的行有完全相同(这里表示的是每个列的值都相同),那么union会将相同的行合并,最终只保留一行,而union all不会合并相同的行
mysql union使用实例
假设现在有三张表,分别为prospect、customer和vendor,这三张表的结构和数据如下所示。
mysql> SELECT * FROM prospect;
+---------+-------+------------------------+
| fname | lname | addr |
+---------+-------+------------------------+
| Peter | Jones | 482 Rush St., Apt. 402 |
| Bernice | Smith | 916 Maple Dr. |
+---------+-------+------------------------+
/* http://www.manongjc.com/article/1440.html */
mysql> SELECT * FROM customer;
+-----------+------------+---------------------+
| last_name | first_name | address |
+-----------+------------+---------------------+
| Peterson | Grace | 16055 Seminole Ave. |
| Smith | Bernice | 916 Maple Dr. |
| Brown | Walter | 8602 1st St. |
+-----------+------------+---------------------+
mysql> SELECT * FROM vendor;
+-------------------+---------------------+
| company | street |
+-------------------+---------------------+
| ReddyParts, Inc. | 38 Industrial Blvd. |
| Parts-to-go, Ltd. | 213B Commerce Park. |
+-------------------+---------------------+
现在我们要对这三张表进行union联合查询,sql语句如下:
mysql> SELECT fname, lname, addr FROM prospect
-> UNION
-> SELECT first_name, last_name, address FROM customer
-> UNION
-> SELECT company, \'\', street FROM vendor;
+-------------------+----------+------------------------+
| fname | lname | addr |
+-------------------+----------+------------------------+
| Peter | Jones | 482 Rush St., Apt. 402 |
| Bernice | Smith | 916 Maple Dr. |
| Grace | Peterson | 16055 Seminole Ave. |
| Walter | Brown | 8602 1st St. |
| ReddyParts, Inc. | | 38 Industrial Blvd. |
| Parts-to-go, Ltd. | | 213B Commerce Park. |
+-------------------+----------+------------------------+
因为vendor只有两列,其他两种表有三列,但union联合查询必须拥有相同的列数,所有在查询vendor表时,我们增加了一个空列(SELECT company, '', street FROM vendor;)
如果想选择所有记录,包括重复的,请使用UNION ALL关键字:
mysql> SELECT fname, lname, addr FROM prospect
-> UNION ALL
-> SELECT first_name, last_name, address FROM customer
-> UNION
-> SELECT company, \'\', street FROM vendor;
+-------------------+----------+------------------------+
| fname | lname | addr |
+-------------------+----------+------------------------+
| Peter | Jones | 482 Rush St., Apt. 402 |
| Bernice | Smith | 916 Maple Dr. |
| Grace | Peterson | 16055 Seminole Ave. |
| Bernice | Smith | 916 Maple Dr. |
| Walter | Brown | 8602 1st St. |
| ReddyParts, Inc. | | 38 Industrial Blvd. |
| Parts-to-go, Ltd. | | 213B Commerce Park. |
+-------------------+----------+------------------------+ 继续阅读与本文标签相同的文章
下一篇 :
DevOps平台之开源技术图谱
-
手机信号突然从“4G”变成“E”,是什么意思?客服给出答案
2026-05-14栏目: 教程
-
互联网架起“乌镇式生活”
2026-05-14栏目: 教程
-
微信才是内存“杀手”,别再乱清理,关闭这个功能,手机立马流畅
2026-05-14栏目: 教程
-
手机信号变成E是什么意思?看完专业人士给出的解释后,涨知识了
2026-05-14栏目: 教程
-
关于Word的四个隐藏办公小技巧,你可能一个也不知道,建议收藏!
2026-05-14栏目: 教程
