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