2011-09-19 43 views
1

我需要连接两个表。如果b列为空,则连接将在c列上完成。如果不是,连接将在b列上。布尔归案

这工作正如我所需要的。但我怀疑,我失去了一些东西,因为它看起来有点令人费解了它做什么:

select * 
from the_table t 
inner join another_table a 
    on 
    case when a.b = '' then 
     case when t.c = a.c then 1 else 0 end 
    else 
     case when t.b = a.b then 1 else 0 end 
    end = 1 

我缺少的东西?

回答

7
ON (a.b = '' AND t.c = a.c) OR (a.b <> '' AND t.b = a.b)