结合连接

时间:2011-01-10 16:05:11

标签: sql join

我的X表有设施名称和制造商名称,但我需要将它们显示为facilityID和manufacturerID。

我写了两个连接来显示它们:

第一个显示设施ID:

select T1.facilityID, t2.*
from lkuFacility t1 right join X t2 on t1.facilityName = t2.facility
and t1.siteCode = t2.siteID
order by siteid

第二个显示制造商ID:

select T1.manufacturerID, t2.*
from lkuManufacturer t1 right join X t2 on t1.manufacturerName = t2.manufacturer
order by manufacturerid

如何将它们放入一个查询中,在一个结果屏幕上显示设施ID和制造商ID?

3 个答案:

答案 0 :(得分:6)

如果您希望根据表格X关联它们,请尝试以下操作:

SELECT f.facilityID, m.manufacturerID, X.*
FROM X
LEFT JOIN lkuFacility AS f
ON f.facilityname = X.facility AND f.siteCode = X.siteID
LEFT JOIN lkiManufacturer AS m
ON m.manufacturerName = X.manufacturer

修改
如果您想将它们连接到相同的结果集但不是并排(如上所述),您应该能够使用UNION运算符,如下所示:

select T1.facilityID, t2.*
from lkuFacility t1 right join X t2 on t1.facilityName = t2.facility
and t1.siteCode = t2.siteID
UNION ALL
select T1.manufacturerID, t2.*
from lkuManufacturer t1 right join X t2 on t1.manufacturerName = t2.manufacturer
order by manufacturerid

答案 1 :(得分:0)

您可以使用UNION ALL运算符来连接两个查询。

答案 2 :(得分:0)

如果你不介意它们分开,那么做一个UNION ALL语句。如果你想要两个在同一行,你可能需要两个连接到X表。