gpt4 book ai didi

sql - 组合连接

转载 作者:行者123 更新时间:2023-12-05 00:39:16 27 4
gpt4 key购买 nike

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

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

第一个显示设施 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?

最佳答案

如果您希望它们基于表 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

关于sql - 组合连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4648753/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com