2011-09-19 44 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)