使用不同的不同表连接选择查询的结果

时间:2018-06-12 21:50:25

标签: mysql sql join

我只是在查询表格

select label,  proces,  product from Signalering sig

我想要添加到此查询中的是有机会使用3个不同的表来加入Select的结果。假设仅加入1个表的情况。查询看起来像:

select label,  proces,  product from Signalering sig
JOIN ScreeningLabelAuthorizationLock p
ON sig.label = p.Value

现在我想以同样的方式加入,同时选择2个表的结果。在我看来,它看起来像这样:

select label,  proces,  product from Signalering sig
JOIN ScreeningLabelAuthorizationLock p
ON sig.label = p.Value
JOIN ScreeningProcessAuthorizationLock q
ON sig.proces = q.Value
JOIN ScreeningProducthAuthorizationLock s
ON sig.proces = s.Value

结果不是预期的结果,因为连接对前面的结果起作用,而我希望它从Select语句开始工作。提前谢谢!

编辑:以下查询给出了正确的结果:

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
INNER JOIN ScreeningProcessAuthorizationLock q
ON sig.proces = q.Value
union
select [signaleringid], label,  proces,  product from Signalering sig
INNER JOIN ScreeningProductAuthorizationLock r
ON sig.product = r.Value

我正在寻找一种避免3选择查询并使其仅为1的方法。

2 个答案:

答案 0 :(得分:0)

select label,  proces,  product from Signalering sig
LEFT JOIN ScreeningLabelAuthorizationLock p
ON sig.label = p.Value
LEFT JOIN ScreeningProcessAuthorizationLock q
ON sig.proces = q.Value
LEFT JOIN ScreeningProducthAuthorizationLock s
ON sig.proces = s.Value

答案 1 :(得分:0)

鉴于您的样本数据和预期结果,我相信您希望使用union all

select s2.*
from Signalering s 
    join ScreeningLabelAuthorizationLock s2 on s.label = s2.value
union all
select s2.*
from Signalering s 
    join ScreeningProcessAuthorizationLock s2 on s.proces = s2.value