HQL表连接
前言
更多往期文章请查看
大数据学习目录
Hive支持通常的SQL JOIN语句,仅支持等值连接,不支持非等值连接。
JOIN 语句中经常会使用表的别名。使用别名可以简化SQL语句的编写,使用表名前
缀可以提高SQL的解析效率。
连接查询操作分为两大类:内连接和外连接,而外连接可进一步细分为三种类型:
- 内连接: [inner] join
- 外连接 (outer join)
- 左外连接。 left [outer] join,左表的数据全部显示
- 右外连接。 right [outer] join,右表的数据全部显示
- 全外连接。 full [outer] join,两张表的数据都显示
案例演示
准备数据
--u1.txt
1,a
2,b
3,c
4,d
5,e
6,f
--u2.txt
4,d
5,e
6,f
7,g
8,h
9,i
建表
--u1 表
create table if not exists u1( id int, name string)
row format delimited fields terminated by ',';
--u2表
create table if not exists u2( id int, name string)
row format delimited fields terminated by ',';
倒入数据
load data local inpath '/opt/data/u1.txt' into table u1;
load data local inpath '/opt/data/u2.txt' into table u2;
内连接
select * from u1 join u2 on u1.id = u2.id;
左外连接
select * from u1 left join u2 on u1.id = u2.id;
右外连接
select * from u1 right join u2 on u1.id = u2.id;
全外连接
select * from u1 full join u2 on u1.id = u2.id;
笛卡尔积
满足以下条件将会产生笛卡尔集:
- 没有连接条件
- 连接条件无效
- 所有表中的所有行互相连接
如果表A、B分别有M、N条数据,其笛卡尔积的结果将有 M*N 条数据;缺省条件下hive不支持笛卡尔积运算;
set hive.strict.checks.cartesian.product=false;
select * from u1,u2;