[英]Dynamically decide join table based on column value
I want to dynamically inner join tables, here is my SQL query 我想动态内连接表,这是我的SQL查询
Update temp
Set temp.Order_Id = parent.ID
from #TempTransactions AS temp
Inner Join (case when temp.OrderType = 1 then preorders else orders end) AS parent
ON parent.Cloud_Id = temp.Order_Id
Is it possible I can decide in above way or any other alternative? 我可以通过上述方式或任何其他选择来决定吗?
If yes, how? 如果有,怎么样?
Two Left joins would do. 两个左连接会这样做。
Update temp Set temp.Order_Id = COALESCE(p.ID, o.ID)
from #TempTransactions AS temp
LEFT Join preorders p ON p.Cloud_Id = temp.Order_Id AND temp.OrderType=1
LEFT JOIN orders o ON o.Cloud_Id = temp.Order_Id AND (temp.OrderType <> 1 OR temp.OrderType IS NULL)
WHERE COALESCE(p.ID, o.ID) IS NOT NULL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.