我在 hive 中创建了如下两个表
创建表test1(id string)
;
create table test2(id string);
test1 的值如下所示
1
1
test2 的值如下所示
1
1
当我加入这两个表时,我得到了输出
1
1
1
1
这是使用的查询:
select a.id from test1 a,test2 b where a.id=b.id;
请帮助我希望输出为
1
1
我正在使用cloudera 发行版
最佳答案
最好使用 ANSI 连接语法:
select a.id
from test1 a
inner join test2 b on a.id=b.id
预期的输出不能是您的联接的结果,因为对于每个 a.id
,所有来自 a
和 b
的匹配行都被选中。对于 a
的第一行,它将是 b
中的两个匹配行。对于 a
的第二行,它也是来自 b
的两个匹配行。所以总共有四行。
例如,您可以在连接之前对第二个表应用 distinct。
select a.id
from test1 a
inner join (select distinct b.id from test2 b) b on a.id=b.id
在这种情况下,对于表 a
中的每一行,它将是表 b
中的单个匹配行。
请参阅本类(class)以更好地理解 JOINS:https://www.coursera.org/learn/analytics-mysql/lecture/kydcf/joins-with-many-to-many-relationships-and-duplicates
关于sql - Hive 加入理解问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45812801/