【问题标题】:Controlling join order in combining left join and inner join in Impala SQL在 Impala SQL 中控制左连接和内连接的连接顺序
【发布时间】:2017-12-12 11:57:51
【问题描述】:

有了这种数据

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_b 和 table_c 的内连接首先发生,并且仅在 table_a 和 (table_b 内连接到 table_b) 之间进行左连接之后。

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

【问题讨论】:

  • 连接链从左到右进行评估。使用括号。
  • 您的请求毫无意义。 b 和 c 之间的 on 条件将把 left join 变成内部连接。您应该提供样本数据和期望的结果。
  • 我添加了数据样本和想要的结果。我从来没有用过括号。

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


【解决方案1】:

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

    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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 2017-08-15
    • 2015-03-04
    • 2018-07-31
    相关资源
    最近更新 更多