Tsql Sybase-基于一列的值对列进行条件联接
在这种情况下,我需要根据其中一个表上的列的值进行条件连接 表a 连接上的表_b tb 案例 当ta.column_1=1时,则ta.column_2=tb.column_2 当ta.column_1=2时,则ta.column_2=tb.column_2和ta.column_3=tb.column_3 当ta.column_1=3时,则ta.column_2=tb.column_2和ta.column_3=tb.column_3和ta.column_4=tb.column_4 终止 请告诉我该怎么做 尝试搜索并获得了一些使用左连接的选项,但我不确定如何执行此操作: 请告知。在选择列时,您可以使用左连接并使用大小写表达式:Tsql Sybase-基于一列的值对列进行条件联接,tsql,join,conditional,sybase,Tsql,Join,Conditional,Sybase,在这种情况下,我需要根据其中一个表上的列的值进行条件连接 表a 连接上的表_b tb 案例 当ta.column_1=1时,则ta.column_2=tb.column_2 当ta.column_1=2时,则ta.column_2=tb.column_2和ta.column_3=tb.column_3 当ta.column_1=3时,则ta.column_2=tb.column_2和ta.column_3=tb.column_3和ta.column_4=tb.column_4 终止 请告诉我
select
case when ta.column_1 = 1 then b1.column
case when ta.column_1 = 2 then b2.column
case when ta.column_1 = 3 then b3.column
end
from table_a ta
left join table_b b1 on ta.column_2 = tb.column_2
left join table_b b2 on ta.column_2 = tb.column_2 and ta.column_3 = tb.column_3
left join table_b b3 on ta.column_2 = tb.column_2 and ta.column_3 = tb.column_3 and ta.column_4 = tb.column_4
我认为这一切都可以表述为单一的布尔表达式
...
join table_b tb on
(ta.column_1 = 1 and ta.column_2 = tb.column_2) or
(ta.column_1 = 2 and ta.column_2 = tb.column_2 and ta.column_3 = tb.column_3)
...
尝试一个联盟
select * from tbla ta, tblb tb
where ta.column_1 = 1
and ta.column_2 = tb.column_2
union
select * from tbla ta, tblb tb
where ta.column_1 = 2
and ta.column_2 = tb.column_2
and ta.column_3 = tb.column_3
union
select * from tbla ta, tblb tb
where ta.column_1 = 3
and ta.column_2 = tb.column_2
and ta.column_3 = tb.column_3
and ta.column_4 = tb.column_4
tada非常优雅的解决方案。非常感谢。