从单选多个联接



我有以下查询:

select [signaleringid], label,  proces,  product from Signalering sig
JOIN ScreeningLabelAuthorizationLock p
ON sig.label = p.Value
union
select [signaleringid], label,  proces,  product from Signalering sig
JOIN ScreeningProcessAuthorizationLock q
ON sig.proces = q.Value
union
select [signaleringid], label,  proces,  product from Signalering sig
JOIN ScreeningProductAuthorizationLock r
ON sig.product = r.Value

如何在不重复 3 次相同的 Select 查询的情况下获得相同的结果?我想要的只是 1 个选择查询,我从中分别连接到 3 个不同的表,但最后结果都在一起。

编辑:

Signalering table: [label][proces][product]
ScreeningLabelAuthorizationLock table: [Value][Lock]
ScreeningProcessAuthorizationLock: [Value][Lock]
ScreeningProductAuthorizationLock: [Value][Lock]

由于您只使用表中的列Signalering因此无需使用 join 即可获得此结果。一种解决方案是检查给定的值是否存在于相应的表中。然后将现有表达式与 where 子句中的 or 语句组合在一起。这样,您只会获得 Signalering 行,这些行将满足一个或多个存在的表达式。

select sig.[signaleringid], sig.label,  sig.proces,  sig.product 
from Signalering sig
where sig.label exists (select Value from ScreeningLabelAuthorizationLock)
or sig.proces exists (select Value from ScreeningProcessAuthorizationLock)
or sig.product exists (select Value from ScreeningProductAuthorizationLock)
group by sig.[signaleringid], sig.label,  sig.proces,  sig.product

另一种选择可能是使用左外连接

select sig.[signaleringid], sig.label,  sig.proces,  sig.product 
from Signalering sig
left outer join ScreeningLabelAuthorizationLock la on sig.label = la.Value
left outer join ScreeningProcessAuthorizationLock lp on sig.proces = lp.Value
left outer join ScreeningProductAuthorizationLock lpa on sig.product = lpa.Value
group by sig.[signaleringid], sig.label,  sig.proces,  sig.product

最新更新