2016-08-17 61 views
0

我早就蜂房的查询,其中有10个连接和大量的条件,下面是3个条件加入基于条件

1) If id is not equal to XFG or GHT, use field sid 
join ABC_Tables on sid 
join CDE_Tables on sid 
2) If id is equal to XFG or GHT, Tested is null, use field pid 
join ABC_Tables on kid 
join CDE_Tables on kid 
3) If id is equal to XFG or GHT, Tested is not null, use field pid 
join ABC_Tables on kid 
join CDE_Tables on kid 

我在做什么,

select 1 conditions 
union all 
select 2 conditions 
union all 
select 3 conditions 

我在做正确的。以上问题的替代方法是什么?

+1

请提供您尝试过的SQL,表的结构,示例数据和该示例的预期结果。请注意,首先查看您的条件2和条件3看起来过于相似以作为单独的条件提及。 – trincot

回答

0

您的条件允许为ON加入条件的一部分。 Hive (ID!='XFG')and(ID!='GHT')and(a.PID=b.PID)允许等于/不等于常量允许连接条件。 a.ID not in ('XFG', 'GHT') and a.sid=b.sid也应该工作:

select * 
    from a 
    left join b on a.ID not in ('XFG', 'GHT') and a.sid=b.sid 
    left join b on a.ID in ('XFG', 'GHT') and Tested is null and a.pid=b.pid