select table1.username, table1.country
from table1 left join table2
on table1.username=table2.username and table1.country=table2.country
where table2.username is NULL and table2.country is NULL;
SELECT t1.username, t1.country
FROM
(select username, country from table1) t1
LEFT JOIN
(Select username, country from table2) t2
ON t1.username =t2.username
AND t1.country =t2.country
WHERE t1.username IS NULL
小鬼note:please do 将where子句用于null操作,而不是and after联接条件这将产生不同的结果。
create table table1 (username string, country string);
create table table2 (username string, country string);
insert into table1 values ('Danny','USA'),('Danny','USA'),('David','UK');
insert into table2 values ('David','UK'),('Michal','France');
select username, country from table1
minus
Select username, country from table2
;
+--------------+-------------+
| _u1.username | _u1.country |
+--------------+-------------+
| Danny | USA |
+--------------+-------------+
在旧的配置单元版本中,您可以使用-
select username
,country
from ( select 1 tab,username, country from table1
union all select 2 tab,username, country from table2
) t
group by username
,country
having count(case when tab = 2 then 1 end) = 0
;
+----------+---------+
| username | country |
+----------+---------+
| Danny | USA |
+----------+---------+
3条答案
按热度按时间xzv2uavs1#
您可以使用左连接,如下所示
u0njafvf2#
是的,因为减号和exist通常不在Hive中工作,所以我们可以通过左下方的连接条件来进行减号操作。
小鬼note:please do 将where子句用于null操作,而不是and after联接条件这将产生不同的结果。
j8yoct9x3#
自hive 2.3.0(于2017年7月17日发布)起,支持集合操作(除并集外还支持减号/除号/交集)
https://issues.apache.org/jira/browse/hive-12764
演示
在旧的配置单元版本中,您可以使用-