[英]joining multiple table in ms sql server
![expected result that i want ] 1 ![我想要的预期结果] 1
select
bd.BuildingID ,
bd.[Building Description] ,
fl.Floor ,
rm.Room,
rm.[Room Description],
rm.[Room Type],
asse.AssetName,
cmp.ComponentName
from Buildings bd
join Floors fl
on bd.BLDID = fl.BLDID
join Rooms rm
on fl.FLID = rm.FLID
join Assets asse
join Components cmp
on cmp.AssetID=asse.AssetID;
I am trying to join multiple tables in ms sql server. 我正在尝试在ms sql服务器中加入多个表。 I have 5 tables: 我有5张桌子:
I want records from multiple tables. 我想要多个表中的记录。
bd.BLDID = fl.BLDID
as I want bd.BuildingID
and bd.[Building Description]
and both of them have common BLID
. 我要加入bd.BLDID = fl.BLDID
因为我想要bd.BuildingID
和bd.[Building Description]
并且它们都有共同的BLID
。 fl.FLID = rm.FLID
as I want rm.Room
, rm.[Room Description]
, and rm.[Room_Type]
我加入了fl.FLID = rm.FLID
因为我想要rm.Room
, rm.[Room Description]
和rm.[Room_Type]
asse.AssetName
and cmp.ComponentName
(both tables have a common AssetID
) 我想要加入资产和组件,也想要asse.AssetName
和cmp.ComponentName
(两个表都有一个共同的AssetID
) I know the query above won't work because of the last 2 lines. 我知道上述查询由于最后两行而无法使用。
Can anyone suggest how this join could be accomplished? 谁能建议如何实现这种加入?
Per comments, we'd need to know which fields were available in your database to fully answer your question. 根据评论,我们需要知道数据库中哪些字段可用才能完全回答您的问题。
At a guess, I'd say assets are likely held in a room, so there's likely to be a RoomId
value (or RMID
given the naming convention that seems to be in place) on the assets. 猜测一下,我认为资产很可能保存在一个房间中,因此资产上可能有一个RoomId
值(或在RoomId
命名约定的情况下为RMID
)。
If that's the case, something like this should work: 如果是这种情况,则应执行以下操作:
select bd.BuildingID
, bd.[Building Description]
, fl.Floor
, rm.Room
, rm.[Room Description]
, rm.[Room Type]
, asse.AssetName
, cmp.ComponentName
from Buildings bd
inner join Floors fl
on bd.BLDID = fl.BLDID
inner join Rooms rm
on fl.FLID = rm.FLID
inner join Assets asse
on asse.RMID = rm.RMID --added this line
inner join Components cmp
on cmp.AssetID=asse.AssetID;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.