Union 语法
select_statement UNION ALL select_statement UNION ALL select_statement ...
UNION用于联合多个SELECT语句的结果集,合并为一个独立的结果集。当前只能支持UNION ALL(bag union)。不消除重复行。每个select语句返回的列的数量和名字必须一样,否则,一个语法错误会被抛出。
如果必须对UNION的结果做一些额外的处理,整个语句可以被嵌入在FROM子句中,象下面这样
SELECT *
FROM (
select_statement
UNION ALL
select_statement
) unionResult
例如,如果我们假设有两个不同的表来跟踪哪个用户发布视频哪个用户发表了评论,以下的查询结果,联合所有的用户表,为用户所有视频发布和评论发布事件,创建一个单一的注释流。
SELECT u.id, actions.date FROM ( SELECT av.uid AS uid FROM action_video av WHERE av.date = '2008-06-03' UNION ALL SELECT ac.uid AS uid FROM action_comment ac WHERE ac.date = '2008-06-03' ) actions
JOIN users u ON (u.id = actions.uid)
翻译自 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Union