一、Union联合查询
- lUNION用于将来自于多个SELECT语句的结果合并为一个结果集。
- 使用DISTINCT关键字与只使用UNION默认值效果一样,都会删除重复行。1.2.0之前的Hive版本仅支持UNION ALL,在这种情况下不会消除重复的行。
- 使用ALL关键字,不会删除重复行,结果集包括所有SELECT语句的匹配行(包括重复行)。
- 每个select_statement返回的列的数量和名称必须相同。
select_statement
UNION [ALL | DISTINCT]
select_statement
UNION [ALL | DISTINCT]
select_statement ...;
二、Union实验
1、现在有student和student_union两个表如下
注:Student_union多了一个学生小陈
2、使用两个表联合查询得到下面结果
select id,name from student union select id,name from students_union;
3、from子句中的子查询
select id,name from (select id,name from student union select id,name from students_union) temp;
注:一定要加别名否则会报错