hive 中 in与not in

在mysql中想表达包含与不包含可以用 in/exists,但是在hive1的版本中是不支持这种语法的,hive2支持这种语法但是效率极低,这里给出一种解决方案。

建表与导入数据

create table if not exists parent(parent_id int,pname string,comments string) row format delimited fields terminated by "\t";
load data local inpath "/home/hadoop/hive_data/parent.txt" into table parent;

数据

parent_id    pname    comments

1    Dan    so what    
2    Jack    who cares
3    Rose    yeah right

建表与导入数据

parent_id    person_id    cname    comments

create table if not exists children(parent_id int,person_id int,cname string,comments string) row format delimited fields terminated by "\t";
load data local inpath "/home/hadoop/hive_data/children.txt" into table children;

数据

1    2    annne    who cares
1    1    julia    yeah right
2    1    marcella    so what
4    3    alice    yeah right

例1:找出父母资料在表中的孩子的名字

in 查询方式

select cname 
from children a left outer join parent b on a.parent_id=b.parent_id
where b.comments is not null;

查询结果

cname
annne
julia
marcella

例2:找出父母资料不在表中的孩子的名字

not in查询方式

select cname 
from children a left outer join parent b on a.parent_id=b.parent_id
where b.comments is null;

查询结果

cname
alice

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值