码迷,mamicode.com
首页 > 数据库 > 详细

[20]SQL UNION 操作符

时间:2017-11-11 23:00:50      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:jpg   相同   tran   sql   sites   结果   http   mys   load   

[20]SQL UNION 操作符

SQL UNION 操作符合并两个或多个 SELECT 语句的结果。

请注意,UNION 内部的每个 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 SELECT 语句中的列的顺序必须相同。 

SQL UNION 语法

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

注释:默认地,UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL。

SQL UNION ALL 语法

SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2

注释:UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。

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

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

下面是 "apps" APP 的数据:

mysql> SELECT * FROM apps;
+----+------------+-------------------------+---------+
| id | app_name   | url                     | country |
+----+------------+-------------------------+---------+
|  1 | QQ APP     | http://im.qq.com/       | CN      |
|  2 | 微博 APP | http://weibo.com/       | CN      |
|  3 | 淘宝 APP | https://www.taobao.com/ | CN      |
+----+------------+-------------------------+---------+
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 输出结果如下:

技术分享

 

select country from websites union select country from apps;

--连接两个表的查询结果集,重复的不显示

select country from websites union all select country from apps order by country;

--连接俩个个表的查询结果集,显示重复

select country,name from websites where country = CN union all 

select country,app_name from apps where country=CN order by name; 

--通过where条件查询的结果,连接连个表的结果集,并根据名字排序。

 

 

[20]SQL UNION 操作符

标签:jpg   相同   tran   sql   sites   结果   http   mys   load   

原文地址:http://www.cnblogs.com/yinghuali/p/7820151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!