我必须在表之间执行'left'或'inner'连接操作取决于我的存储过程中的输入参数。我知道如何做到这一点非常简单:
if flag = 0
begin
select t1.*, t2.* from t1
inner join t2 on t2.id=t1.id
end
else
begin
select t1.*, t2.* from t1
left join t2 on t2.id=t1.id
end
还有更多解决方案吗?谢谢
答案 0 :(得分:3)
你可以通过做这样的事情只使用左连接来做到这一点
select t1.*, t2.* from t1
left outer join t2 on t2.id=t1.id
WHERE flag = 1 OR t2.id IS NOT NULL
答案 1 :(得分:0)
你没有提到这种语言,所以也许是这样的:
select t1.*, t2.* from t1
left join t2 on t2.id=t1.id
if flag = 0
begin
where t2.id is not null
end
答案 2 :(得分:0)
select t1.*, t2.*
from t1
inner join t2 on t2.id = t1.id
where flag = 0
UNION
select t1.*, t2.*
from t1
left outer join t2 on t2.id = t1.id
where coalesce(flag, 1) <> 0;