select col1
from table1
case when @col2 is null then left outer join else join end
table2 on (join condition)
以上是我的查詢,我想根據1條件選擇左外連接還是右外連接。CASE聲明在加入
是否有落實上述問題的更好的解決方案
select col1
from table1
case when @col2 is null then left outer join else join end
table2 on (join condition)
以上是我的查詢,我想根據1條件選擇左外連接還是右外連接。CASE聲明在加入
是否有落實上述問題的更好的解決方案
select col1
from table1
left outer join
table2 on (join condition)
where @col2 is null or (@col2 is not null and table2.id is not null)
這將left outer
或inner join
之間根據病情選擇。
我不知道這其實是可以以上述方式做...我會寫這樣的事情;所以你根據你的附加條件短路一個JOIN。
select col1
from table1
left outer join table2
on (condition)
and @col2 is null
right outer join table2
on (condition)
and @col2 is not null
使用此結構:
select *
from (
select
Key = 1,
-- remainder of left outer join
union all
select
Key=2,
-- remainder of right outer join
) T
where Key = case when (condition) then 1 else 2 end
select col1
from table1 t1 full join table2 t2 on (join condition)
where case when @col2 is null then t2.col1 else t1.col1 end IS NOT NULL
你可以試試這個代碼。
呃,什麼?.............你不能那樣做。請說明您正在嘗試解決的實際問題,而不是一個可感知的解決方案。 – 2013-03-06 08:39:58
你爲什麼要實現一個問題? – Blizzer 2013-03-06 08:40:52
這是(除了是不正確的)在左外連接和內連接之間選擇,但你敘述說右外連接。這是什麼? – 2013-03-06 08:41:05