是否可以在 hive sql 中使用 IF 语句来确定使用哪个 JOIN 语句?

Is it possible to use IF statement in hive sql to determine which JOIN statement to use?

我想使用之前的 select 结果来确定要使用哪个连接语句,如下所示:

select a.a1,
    b.b1,
    c.c3
from table_a a
left join table_b b
on a.a_join_column = b.b_join_column
if (b.b1 = 1,
    left join (
        select c1 as c_join_column, c3
        from table_c
        where c4 is null
    ) c
    on a.tid = d.tid,
    left join (
        select c2 as c_join_column, c3
        from table_c
        where c4 is not null
    ) c
    on a.a_join_column = c.c_join_column,
)

但是当我执行此 sql 时,它会说类似

FAILED: ParseException line 7:0 cannot recognize input near 'if' '(' 'hit_type' in expression specification

如果违反了sql语法,如何实现我的目标?提前致谢!

只需将所有条件放在 ON 子句中即可。使用 ANDOR 和括号使连接条件正确。

select a.a1, b.b1, c.c3
from table_a a
left join table_b b on a.a_join_column = b.b_join_column
left join table_c c on b.b1 = 1
                    and
                    (
                      (c.c1 = a.a_join_column and c.c4 is null)
                      or 
                      (c.c2 = a.a_join_column and c.c4 is not null)
                    )

更新: 您已使用 MySQL 和 Hive 标记您的请求。但是您没有使用 MySQL 并且您说 Hive 在连接条件中不支持 OR 。在这种情况下,将查询分成两部分,将结果与 UNION ALL:

粘合在一起
select a.a1, b.b1, c.c3
from table_a a
left join table_b b on a.a_join_column = b.b_join_column
left join table_c c on b.b1 = 1 and c.c1 = a.a_join_column and c.c4 is null
union all
select a.a1, b.b1, c.c3
from table_a a
left join table_b b on a.a_join_column = b.b_join_column
left join table_c c on b.b1 = 1 and c.c2 = a.a_join_column and c.c4 is not null