左连接还是内连接取决于参数

发布于 2024-11-17 17:57:48 字数 260 浏览 2 评论 0原文

我必须在表之间进行“左”或“内”连接操作,具体取决于存储过程中的输入参数。我知道该怎么做很简单:

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 

还有更多的解决方案吗?谢谢

I have to do 'left' or 'inner' join operation between tables depends on input parameter in my stored procedure. I know how to do it very simply:

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 

Is there any more solution ? Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖树树初阳… 2024-11-24 17:57:48

您可以通过执行以下操作仅使用左连接来完成此操作

select t1.*, t2.* from t1 
 left outer join t2 on t2.id=t1.id 
WHERE flag = 1 OR t2.id IS NOT NULL

you can do it using only left join by doing something like this

select t1.*, t2.* from t1 
 left outer join t2 on t2.id=t1.id 
WHERE flag = 1 OR t2.id IS NOT NULL
旧竹 2024-11-24 17:57:48

你没有提到语言,所以也许是这样的:

select t1.*, t2.* from t1 
left join t2 on t2.id=t1.id 

if flag = 0
begin 
 where t2.id is not null
end

You didn't mention the language, so maybe something like this:

select t1.*, t2.* from t1 
left join t2 on t2.id=t1.id 

if flag = 0
begin 
 where t2.id is not null
end
提赋 2024-11-24 17:57:48
 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;
 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文