如何从hive的两张表中获取不匹配的记录?

7d7tgy0s  于 2021-04-08  发布在  Hive
关注(0)|答案(1)|浏览(886)

我有以下两张数据表,我只需要使用hive获取未匹配的数据记录。
表1:

hive> select * from dept;
OK
10      ACCOUNTING      NEW YORK
20      RESEARCH        DALLAS
30      SALES   CHICAGO
40      OPERATIONS      BOSTON

表2:

hive> select * from dept_text;
OK
10      ACCOUNTING      NEW YORK
20      RESEARCH        DALLAS
30      SALES   CHICAGO
40      OPERATIONS      BOSTON
50      Software        Bangalore
60      Housewife       yellandu

我需要得到下面这样的输出,谁能帮帮我?

50      Software        Bangalore
60      Housewife       yellandu
rjjhvcjd

rjjhvcjd1#

在dept_text表上使用 "左连接",然后只过滤dept表的null id列。

select dt.* from dept_text dt 
    left join 
dept d 
  on d.id=dt.id 
where d.id is null;

例子:`

desc dept;
--id                    int
--desc                  string
--city                  string

select * from dept;
--OK
--dept.id   dept.desc   dept.city
--10    ACCOUNTING  NEW YORK
--20    RESEARCH    DALLAS
--30    SALES   CHICAGO
--40    OPERATIONS  BOSTON

--if you want to join on desc column

select dt.* from dept_text dt 
left join 
dept d 
on d.desc=dt.desc 
where d.id is null;

--or if you want to join on id column

select dt.* from dept_text dt 
left join 
dept d 
on d.id=dt.id 
where d.id is null;

相关问题