实训4:union 联合查询

Posted Smartloe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实训4:union 联合查询相关的知识,希望对你有一定的参考价值。

一、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两个表如下

image-20210629171007902

注:Student_union多了一个学生小陈

2、使用两个表联合查询得到下面结果

select id,name from student union select id,name from students_union;

image-20210629172958382

3、from子句中的子查询

select id,name from (select id,name from student union select id,name from students_union) temp;

image-20210629173822594

注:一定要加别名否则会报错

以上是关于实训4:union 联合查询的主要内容,如果未能解决你的问题,请参考以下文章