两个表的不同值

a8jjtwal  于 2021-06-03  发布在  Hadoop
关注(0)|答案(3)|浏览(270)

假设我有如下两张表。

ID, NAME, NO
1, PK,101
2,AK,102
3,OK,103

ID,CITY,COUNTRY
1,NY,US
2,NJ,US

我需要得到这样的输出,只有id值

3

有人帮忙吗。?

b5lpy0ml

b5lpy0ml1#

正如@drago所回答的,在这种情况下,外部连接应该是足够有效的!

rjee0c15

rjee0c152#

基本上可以对表和筛选器行执行完全联接,其中任何表的id都为null。假设您有两个名为temp\u table1和temp\u table2的表,下面的查询应该可以工作:
select*from(select(case when temp_table1.id is null then temp_table2.id when temp_table2.id is null then temp_table1.id else null end)as id from temp_table1 full join temp_table2 on temp_table1.id=temp_table2.id)q1其中id不为null;
说明:首先,我对这两个表执行完全联接,以获得与这两个表对应的所有行。
如果条件是我正在检查,如果id在表1中为null,那么我们将从表2中返回id。
类似地,如果表2中的id为null,我们将从表1返回id。如果它在任何表中都不是null,那么我们将返回null。
最后,我们将其放入子查询中,并返回返回id不为null的行。希望这有帮助。

9fkzdhlc

9fkzdhlc3#

从table1中选择a.id a left outer join table2 b on b.id=a.id其中b.id为空;

相关问题