MySQL UNION 和 UNION all 操作符将两个结果集合并一个表

MySQL UNION 和 UNION all 操作符将两个结果集合并一个表

比如第一个查询有100条两列,第二个查询结果也为160条两列,故使用union all之后,可以将这两个结果合并成一个,变成260行两列。

描述

MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。

语法

MySQL UNION 操作符语法格式:

  1.  
    SELECT expression1, expression2, ... expression_n
  2.  
    FROM tables
  3.  
    [WHERE conditions]
  4.  
    UNION [ALL | DISTINCT]
  5.  
    SELECT expression1, expression2, ... expression_n
  6.  
    FROM tables
  7.  
    [WHERE conditions];

参数

  • expression1, expression2, ... expression_n: 要检索的列。

  • tables: 要检索的数据表。

  • WHERE conditions: 可选, 检索条件。

  • DISTINCT: 可选,删除结果集中重复的数据。默认情况下 UNION 操作符已经删除了重复数据,所以 DISTINCT 修饰符对结果没啥影响。

  • ALL: 可选,返回所有结果集,包含重复数据。


演示数据库

在本教程中,我们将使用 RUNOOB 样本数据库。

下面是选自 "Websites" 表的数据:

  1.  
    mysql> SELECT * FROM Websites;
  2.  
    +----+--------------+---------------------------+-------+---------+
  3.  
    | id | name | url | alexa | country |
  4.  
    +----+--------------+---------------------------+-------+---------+
  5.  
    | 1 | Google | https://www.google.cm/ | 1 | USA |
  6.  
    | 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
  7.  
    | 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
  8.  
    | 4 | 微博 | http://weibo.com/ | 20 | CN |
  9.  
    | 5 | Facebook | https://www.facebook.com/ | 3 | USA |
  10.  
    | 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
  11.  
    +----+---------------+---------------------------+-------+---------+

下面是 "apps" APP 的数据:

  1.  
    mysql> SELECT * FROM apps;
  2.  
    +----+------------+-------------------------+---------+
  3.  
    | id | app_name | url | country |
  4.  
    +----+------------+-------------------------+---------+
  5.  
    | 1 | QQ APP | http://im.qq.com/ | CN |
  6.  
    | 2 | 微博 APP | http://weibo.com/ | CN |
  7.  
    | 3 | 淘宝 APP | https://www.taobao.com/ | CN |
  8.  
    +----+------------+-------------------------+---------+
  9.  
    3 rows in set (0.00 sec)

一、SQL UNION 实例

下面的 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 实例

下面的 SQL 语句使用 UNION ALL 从 "Websites" 和 "apps" 表中选取所有的country(也有重复的值):

实例

SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;

执行以上 SQL 输出结果如下:

 

三、带有 WHERE 的 SQL UNION ALL

下面的 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 语句:用于将不同表中相同列中查询的数据展示出来;(包括重复数据)

使用形式如下:

  1.  
    SELECT 列名称 FROM 表名称 UNION SELECT 列名称 FROM 表名称 ORDER BY 列名称;
  2.  
    SELECT 列名称 FROM 表名称 UNION ALL SELECT 列名称 FROM 表名称 ORDER BY 列名称;

四、我的例子如下:

 

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
posted @ 2022-06-03 20:45  阿波罗任先生  阅读(806)  评论(0编辑  收藏  举报