在Impala SQL中组合左连接和内连接时控制连接顺序

时间:2017-12-12 11:57:51

标签: sql join left-join inner-join impala

使用这种数据

create table table_a as select 1 as id1;
insert into table_a values (2),(3);

create table table_b as select 1 as id1, 'a' as id2;
insert into table_b values (1,'b');

create table table_c as select 'a' as id2;

我在Impala sql中有以下类型的连接:

   select *
     from table_a as a
left join table_b as b
       on b.id1 = a.id1
left join table_c as c
       on c.id2 = b.id2

产生这个结果

"id1","id1","id2","id2"
1,1,b,
1,1,a,a
2,,,
3,,,

我希望第二个连接是内连接而不是左连接:

   select *
     from table_a as a
left join table_b as b
       on b.id1 = a.id1
     join table_c as c   /* <- How to process this join first without using inner queries? */
       on c.id2 = b.id2

得到这个结果:

"id1","id1","id2","id2"
1,1,a,a
2,,,
3,,,

因此,我希望table_btable_c的内部联接首先发生,然后才能在table_a和(table_b内部联接之间进行左联接到table_b)。

是否可以在不使用内部查询的情况下以这种方式确定连接顺序?

1 个答案:

答案 0 :(得分:0)

在@jarlh的帮助下,我实现了从左到右的连接处理,然后发现可以使用RIGHT连接:

    select *
      from table_c as c
      join table_b as b
        on b.id2 = c.id2
right join table_a as a
        on a.id1 = b.id1;

为了获得理想的结果:

"id2","id1","id2","id1"
a,1,a,1
,,,2
,,,3