01 张三 02 李四 03 王五 04 马六 05 小七 06 二狗
01 11 03 33 04 44 06 66 07 77 08 88
01 男 02 男 03 女 04 男 05 女 06 女 07 男 08 X
create table if not exists t_1(id string,name string)row format delimited fields terminated by '\t'; create table if not exists t_2(id string,score string)row format delimited fields terminated by '\t'; create table if not exists t_3(id string,sex string)row format delimited fields terminated by '\t';
load data local inpath '/root/tmp/t_1' into table t_1; load data local inpath '/root/tmp/t_2' into table t_2; load data local inpath '/root/tmp/t_3' into table t_3;
select * from t_1 join t_2; 等价于: select * from t_1,t_2;
select * from t_1 t1 join t_2 t2 on t1.id=t2.id;
图解原理:
mysql
左链接是显示左边的表的全部数据,若是有右边表的数据与之对应,则显示;不然显示nullsql
select * from t_1 t1 left join t_2 t2 on t1.id=t2.id;
图解原理:
code
与左链接相似,右链接是显示右边的表的全部数据,若是有左边表的数据与之对应,则显示;不然显示nullorm
select * from t_1 t1 right join t_2 t2 on t1.id=t2.id;
图解原理:
blog
至关于t_1和t_2的数据都显示,若是没有对应的数据,则显示Null.string
select * from t_1 t1 full outer join t_2 t2 on t1.id=t2.id;
图解原理:it
semi join仅会显示t_1的数据,即左边表的数据。效率比左链接快,由于它会先拿到t_1的数据,而后在t_2中查找,只要查找到结果立马就返回t_1的数据。table
select * from t_1 t1 left semi join t_2 t2 on t1.id=t2.id;
图解原理:form
若是在链接中使用了公共键,Hive还支持经过一次MapReduce来链接多个表。class
select t1.*,t3.sex,t2.score from t_1 t1 join t_3 t3 on t1.id=t3.id join t_2 t2 on t2.id=t1.id;