#学习笔记-sql# union实例及用法

293 阅读1分钟

(默认选取两个表查询结果的不同值,可通过all标识选取全部,或通过distinct清除重复值,union后默认为distinct,可有[all|distinct])

UNION 语句:用于将不同表中相同列中查询的数据展示出来;(不包括重复数据)

UNION ALL 语句:用于将不同表中相同列中查询的数据展示出来;(包括重复数据)

原始表:

·(不展示重复值)
select country from Websites
union
select country from apps

·(展示重复值)
select country from Websites
union all
select country from apps

·查询两个条件的值
SELECT country, name FROM Websites
WHERE country='CN'
UNION ALL
SELECT country, app_name FROM apps
WHERE country='CN'
ORDER BY country;

** 会得出两个表所有country字段的结果



参考:http://www.runoob.com/mysql/mysql-union-operation.html