Один метод использует два left join
s.
select ...
from a
left join b b2 on b2.col2 = a.col2
left join b b3 on b3.col3 = a.col3
where b2.col2 is not null or b3.col3 is not null
Это позволяет избежать «умножения» строк, когда оба условия совпадают. Затем вы можете обратиться к столбцам с coalesce()
, отдав приоритет b2
.
select a.*, coalesce(b2.col4, b3.col4) b_col4, coalesce(b2.col5, b3.col5) b_col5
from a
left join b b2 on b2.col2 = a.col2
left join b b3 on b3.col3 = a.col3
where b2.col2 is not null or b3.col3 is not null