问题描述
我有3张桌子
表1,表2,表3
我想基于公共密钥并基于特定值加入所有这3个表。例如,假设Table1的Column3 ='M',那么我将Table2与table3连接,否则不加入
I want to join all these 3 tables based on common key and based on a particular value. For example , lets say if Column3 of Table1= 'M' then I will join Table2 with table3, otherwise do not join
选择Table1。 Column1,Table1.Column2,Table2.Column1,Table3.column1,Table3.column2等等。
select Table1.Column1,Table1.Column2,Table2.Column1,Table3.column1,Table3.column2 etc. etc.
来自Table1,Table2,Table3
From Table1,Table2,Table3
其中Table1.Column1 = Table2.Column1 和
Where Table1.Column1 = Table2.Column1 and
Table2.Column1 = Table3.Column1
Table2.Column1=Table3.Column1
和Case1.Column3 ='M'时的情况,然后是Table2.Column2 = Table3.Column2 elso不加入
and Case when Table1.Column3 = 'M' then Table2.Column2=Table3.Column2 elso dont join
我该怎么做?
提前致谢
Itsme
推荐答案
根据您的描述,您希望加入两种不同的类型。对吗?
Per your description , you would like to join with two different types. Right ?
请尝试以下脚本。
select Table1.Column1,Table1.Column2,Table2.Column1,Table3.column1,Table3.column2
From Table1
join Table2 on Table1.Column1 = Table2.Column1
join Table3 on Table2.Column1=Table3.Column1 and Table2.Column2=Table3.Column2
where Table1.Column3 = 'M'
union all
select Table1.Column1,Table1.Column2,Table2.Column1,Table3.column1,Table3.column2
From Table1
join Table2 on Table1.Column1 = Table2.Column1
join Table3 on Table2.Column1=Table3.Column1
where Table1.Column3 <> 'M'
Hope它可以帮到你。
Hope it can help you.
最好的问候,
Rachel
这篇关于基于Case语句的简单连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!