比如第一个查询有100条两列,第二个查询结果也为160条两列,故使用union all之后,可以将这两个结果合并成一个,变成260行两列。
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。
MySQL UNION 操作符语法格式:
expression1, expression2, ... expression_n: 要检索的列。
tables: 要检索的数据表。
WHERE conditions: 可选, 检索条件。
DISTINCT: 可选,删除结果集中重复的数据。默认情况下 UNION 操作符已经删除了重复数据,所以 DISTINCT 修饰符对结果没啥影响。
ALL: 可选,返回所有结果集,包含重复数据。
在本教程中,我们将使用 RUNOOB 样本数据库。
下面是选自 "Websites" 表的数据:
下面是 "apps" APP 的数据:
下面的 SQL 语句从 "Websites" 和 "apps" 表中选取所有不同的country(只有不同的值):
SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country;
执行以上 SQL 输出结果如下:
注释:UNION 不能用于列出两个表中所有的country。如果一些网站和APP来自同一个国家,每个国家只会列出一次。UNION 只会选取不同的值。请使用 UNION ALL 来选取重复的值!
下面的 SQL 语句使用 UNION ALL 从 "Websites" 和 "apps" 表中选取所有的country(也有重复的值):
SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;
执行以上 SQL 输出结果如下:
下面的 SQL 语句使用 UNION ALL 从 "Websites" 和 "apps" 表中选取所有的中国(CN)的数据(也有重复的值):
SELECT country, name FROM Websites
WHERE country='CN'
UNION ALL
SELECT country, app_name FROM apps
WHERE country='CN'
ORDER BY country;
执行以上 SQL 输出结果如下:
总之:
UNION 语句:用于将不同表中相同列中查询的数据展示出来;(不包括重复数据)
UNION ALL 语句:用于将不同表中相同列中查询的数据展示出来;(包括重复数据)
使用形式如下:
mysql> select * from product;
+------+--------+
| id | amount |
+------+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
| 4 | 400 |
+------+--------+
4 rows in set (0.00 sec)mysql> select a.* from product a ;
+------+--------+
| id | amount |
+------+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
| 4 | 400 |
+------+--------+
4 rows in set (0.00 sec)mysql> select a.id a_id from product a ;
+------+
| a_id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)mysql> select a.id as a_id from product a ;
+------+
| a_id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.01 sec)mysql> select a.id as a_id,a.amount a_amout from product a ;
+------+---------+
| a_id | a_amout |
+------+---------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
| 4 | 400 |
+------+---------+
4 rows in set (0.00 sec)mysql> select a.id as a_id,a.amount a_amout from product a union select b.id as b_id,b.amount b_amout from product b;
+------+---------+
| a_id | a_amout |
+------+---------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
| 4 | 400 |
+------+---------+
4 rows in set (0.00 sec)mysql> select a.id as a_id,a.amount a_amout from product a union all select b.id as b_id,b.amount b_amout from product b;
+------+---------+
| a_id | a_amout |
+------+---------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
| 4 | 400 |
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
| 4 | 400 |
+------+---------+
8 rows in set (0.01 sec)mysql> select a.id as a_id from product a union all select concat(b.amount,b.id,'#') from product b;
+-------+
| a_id |
+-------+
| 1 |
| 2 |
| 3 |
| 4 |
| 1001# |
| 2002# |
| 3003# |
| 4004# |
+-------+
8 rows in set (0.00 sec)mysql> select a.id as a_id from product a union all select concat_ws('#',b.amount,b.id) from product b;
+-------+
| a_id |
+-------+
| 1 |
| 2 |
| 3 |
| 4 |
| 100#1 |
| 200#2 |
| 300#3 |
| 400#4 |
+-------+
8 rows in set (0.00 sec)
=================
参考:http://www.runoob.com/mysql/mysql-union-operation.html