Mysql 条件3表联接
表1 表2Mysql 条件3表联接,mysql,sql,Mysql,Sql,表1 表2 PropertyId 1 2 3 4 5 6 表3 PropertyID PropertyCode PropList ID 1 2 45 3 4 67 5 6 89 目标表 PropertyID IType 1 3 2 3 3 11 4 11
PropertyId
1
2
3
4
5
6
表3
PropertyID PropertyCode PropList ID
1 2 45
3 4 67
5 6 89
目标表
PropertyID IType
1 3
2 3
3 11
4 11
5 3
6 3
步骤1:连接表1和表3
步骤2:如果
Table 1.PropertyId.Itype=3
,则在Table 1.PropertyId=Table 2.PropertyId
否则
步骤3:如果
Table 1.PropertyId.Itype=11
,则将表1和表3连接到Table 1.PropertyId=Table 2.PropListId
很抱歉重新发布,但希望正确发布问题
我真的很感谢你的帮助。谢谢。也许,像这样的事情:
PropertyID PropertyCode
1 45
2 45
3 67
4 67
5 89
6 89
您的解释和列定义不匹配,请更正它们好吗 多个IF…然后通常转化为CASE,这可能是您想要的:
;
WITH joinedt1
AS ( SELECT a.PropertyId AS PropertyId ,
b.IType AS IType
FROM tab1 a
LEFT JOIN tab3 b ON a.PropertyId = b.PropertyID
)
SELECT A1.PropertyId ,
C.PropList_ID
FROM joinedt1 A1
INNER JOIN TABLE2 C ON A1.PropertyId = C.PropertyId
WHERE A1.IType = 3
UNION
SELECT A2.PropertyId ,
D.PropList_ID
FROM joinedt1 A2
INNER JOIN TABLE2 D ON A2.PropertyId = D.PropListId
WHERE A2.IType = 11
你已经试过什么了吗?另外,这是sql server还是mysql?我在一开始就尝试了这一点,考虑到表1和表3选择了t1.*,t2.PropertyCode FROM Test.dbo.dailyBudgetTextract t1 internal JOIN Test.dbo.dailyPropertyListExtract t2 on t1.PropertyID=t2.PropListId,然后在表1.PropertyID=Table2.PropListId上连接表1和表3。你是说,这里的表1和表2吗?@Lesya是的,我的错,我只是指表1和表2。
表1.PropertyId.Itype
表1没有正确的Itype?嘿,Lesya,如果你能格式化这个,我将不胜感激。对我来说有点难理解。我不知道你所说的格式是什么意思。代码由sql redactor格式化
FROM T1
JOIN T3
ON T1.PropertyID = T3.PropertyID
JOIN T2
ON T1.PropertyID =
CASE T3.Itype
WHEN 3 THEN T2.PropertyId
WHEN 11 THEN T2.PropListId
END