繁体   English   中英

3个表上的外连接

[英]Outer join on 3 tables

我有3个表,Client,Tool和ClientTools。 一个客户端可以有多个工具,因此ClientTools充当数据透视表(仅包含Id)。

对于给定的客户,我想要的是拥有完整的工具列表,以及指示客户端是否具有此工具的标志。

我到目前为止的地方是:

select      t.Id as [ToolId],
            t.Name as [ToolName],
            Cast(case when c.Id is NULL then 0 else 1 end as bit) as [HasThisTool],
from        Tool t
Left join   ClientTools ct
on          t.Id = ct.ToolId
Left Join   Client c
on          ct.ClientId = c.Id

哪个正确地给了我所有工具,但适用于所有客户端(当多个客户拥有此工具时复制工具行)。

但是一旦我使用了接近过滤到所选客户端的地方,我的查询只返回该客户端的行(因此不再进行左连接)。

我尝试添加where c.Id = 123where (c.Id = 123 or c.Id is null)但没有工作。

我错过了什么?

提前致谢 !

在您的查询中,如果您没有检索客户端的名称,则无需加入该表(但这不是真正的问题)。 尝试这个:

select      t.Id as [ToolId],
            t.Name as [ToolName],
            Cast(case when ct.Id is NULL then 0 else 1 end as bit) as [HasThisTool]
from        Tool t
Left join   (SELECT * FROM ClientTools WHERE ClientId = @ClientId) ct
on          t.Id = ct.ToolId

尝试:

select      t.Id as [ToolId],
            t.Name as [ToolName],
            Cast(case when ct.Id is NULL then 0 else 1 end as bit) as [HasThisTool]
from        Tool t
Left join   ClientTools ct
on          t.Id = ct.ToolId and ct.ClientId = @ClientId

如果你切换表的顺序,它应该工作:

select      t.Id as [ToolId],
            t.Name as [ToolName],
            Cast(case when t.Id is NULL then 0 else 1 end as bit) as [HasThisTool],
from        Client c
Left join   ClientTools ct
on          c.Id = ct.ClientId
Left Join   Tool t
on          ct.ToolId = t.Id
where       c.id = 123

实质上,您说客户端(而不是工具)决定了行是否显示在结果集中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM