使用 ID T-SQL 多重连接到单列
Multiple Join to single column using ID T-SQL
我有以下问题。我正在尝试连接三个表:
表A:Record_id、Reference_id、参数
表 B:Doc_id,名称 B
表 C:Doc_id,名称 C
根据 TableA.Param 中的值,我需要 select 通过加入 Reference_id
来自表 B 或 C 的名称
我尝试过使用 "case" 约束,但没有成功 :)
select a.Record_id, a.Reference_id
, case when Param = 'B' then b.NameB
when Param = 'C' then c.NameC
from TableA as a inner join
TableB as b on a.Reference_id = b.Doc_id inner join
TableC as c on a.Reference_id = c.Doc_id
有什么想法吗?
您可以 left join
满足该条件并使用 coalesce()
获取第一个非空名称
select a.Record_id, a.Reference_id,
coalesce(b.NameB, c.NameC) as name
from TableA as a
left join TableB as b on a.Reference_id = b.Doc_id
and Param = 'B'
and b.STAT = 'A'
left join TableC as c on a.Reference_id = c.Doc_id
and Param = 'C'
and c.STAT = 'A'
顺便说一句,您的查询已结束(错过了 end
)。这适用于 left join
s
select a.Record_id, a.Reference_id,
case when Param = 'B' then b.NameB
when Param = 'C' then c.NameC
end as name
from TableA as a
left join TableB as b on a.Reference_id = b.Doc_id and b.STAT = 'A'
left join TableC as c on a.Reference_id = c.Doc_id and c.STAT = 'A'
试试这个:
SELECT a.Record_id, a.Reference_id, b.NameB AS Name
FROM TableA AS a
INNER JOIN TableB AS b ON a.Reference_id = b.Doc_id
WHERE a.Param = 'B'
UNION
SELECT a.Record_id, a.Reference_id, c.NameC AS Name
FROM TableA AS a
INNER JOIN TableC as c ON a.Reference_id = c.Doc_id
WHERE c.Param = 'C'
SELECT a.Record_id, a.Reference_id,V.Name
FROM TableA AS a
inner join
(
SELECT Doc_id id, NameB Name,'B' Type from TableB
UNION ALL
SELECT Doc_id ID, NameC name,'C' Type From TableC
) AS V On V.ID=Reference_id AND Type=Param
我有以下问题。我正在尝试连接三个表:
表A:Record_id、Reference_id、参数
表 B:Doc_id,名称 B
表 C:Doc_id,名称 C
根据 TableA.Param 中的值,我需要 select 通过加入 Reference_id
来自表 B 或 C 的名称我尝试过使用 "case" 约束,但没有成功 :)
select a.Record_id, a.Reference_id
, case when Param = 'B' then b.NameB
when Param = 'C' then c.NameC
from TableA as a inner join
TableB as b on a.Reference_id = b.Doc_id inner join
TableC as c on a.Reference_id = c.Doc_id
有什么想法吗?
您可以 left join
满足该条件并使用 coalesce()
select a.Record_id, a.Reference_id,
coalesce(b.NameB, c.NameC) as name
from TableA as a
left join TableB as b on a.Reference_id = b.Doc_id
and Param = 'B'
and b.STAT = 'A'
left join TableC as c on a.Reference_id = c.Doc_id
and Param = 'C'
and c.STAT = 'A'
顺便说一句,您的查询已结束(错过了 end
)。这适用于 left join
s
select a.Record_id, a.Reference_id,
case when Param = 'B' then b.NameB
when Param = 'C' then c.NameC
end as name
from TableA as a
left join TableB as b on a.Reference_id = b.Doc_id and b.STAT = 'A'
left join TableC as c on a.Reference_id = c.Doc_id and c.STAT = 'A'
试试这个:
SELECT a.Record_id, a.Reference_id, b.NameB AS Name
FROM TableA AS a
INNER JOIN TableB AS b ON a.Reference_id = b.Doc_id
WHERE a.Param = 'B'
UNION
SELECT a.Record_id, a.Reference_id, c.NameC AS Name
FROM TableA AS a
INNER JOIN TableC as c ON a.Reference_id = c.Doc_id
WHERE c.Param = 'C'
SELECT a.Record_id, a.Reference_id,V.Name
FROM TableA AS a
inner join
(
SELECT Doc_id id, NameB Name,'B' Type from TableB
UNION ALL
SELECT Doc_id ID, NameC name,'C' Type From TableC
) AS V On V.ID=Reference_id AND Type=Param