简体   繁体   English

从 5 个表中获取数据

[英]Get Data from 5 tables

i have 5 tables contains city information (Unite contain (but not always) sub unites - sub unites contain (but not always) building - and building contain (but not always) rooms我有 5 个表包含城市信息(Unite 包含(但不总是)子 unites - sub unites 包含(但不总是)建筑物 - 并且建筑物包含(但不总是)房间

1-Unites: includes columns: 1-Unites:包括列:

Id (PK)标识(PK)

UniteName统一名称

.... ....

2- SubUnites: includes columns: 2- SubUnites:包括列:

ID (PK)身份证(PK)

Name姓名

UniteId(FK for ID in Unites table Table)- Note (not all unite contain sub unite) .... UniteId(FK for ID in Unites table Table)- 注意(并非所有的 unite 都包含 sub unite)....

3- Building: includes columns: 3- 建筑:包括列:

ID (PK)身份证(PK)

Name姓名

SubUniteId (FK for ID in SubUnites Table)- Note (not all unite sub unite contain buildings) ... SubUniteId(SubUnites 表中 ID 的 FK)- 注意(并非所有 unite sub unite 都包含建筑物)...

4- Rooms: includes columns : 4- 房间:包括柱子:

ID (PK)身份证(PK)

Name姓名

BuildingId (FK for ID in Building Table)- Note (not all building contain rooms) .... BuildingId(建筑物表中的 ID 的 FK)- 注意(并非所有建筑物都包含房间)....

5- Orders : includes columns: 5- 订单:包括列:

ID ID

AreaId (which may be (ID column from Unite table) or (ID column from SubUnites table) or (ID column from Building table) or (ID column from Rooms table) AreaId(可能是(Unite 表中的 ID 列)或(SubUnites 表中的 ID 列)或(Building 表中的 ID 列)或(Rooms 表中的 ID 列)

i can make order for for any of them by its ID我可以通过它的 ID 为他们中的任何一个下订单

question : how i can make sql select query to get orders for any of them with full information .. which mean if i get orders for a specif room i need to know in which building and in which sub unite and in which unite and if i get orders for a specific building i need to know in which sub untes and in which unites an so on- with out duplicating data问题:我如何进行 sql select 查询以获取具有完整信息的任何订单的订单..这意味着如果我收到特定房间的订单,我需要知道哪个建筑物和哪个子单元以及哪个单元以及如果我获得特定建筑物的订单我需要知道在哪个子单元和哪个单元中等等 - 没有重复的数据

i tried many queries but not working我尝试了很多查询但没有工作

any help任何帮助

SELECT A.ID as unitid,A.Unitname as unitname,B.id as subuniteid,B.Name as subunitname,c.ID as buildingid,C.Name as buildingname,D.id as roomid,D.name as roomname,e.id as orderid,e.areaid
from
UniT A JOIN Subunit B
on A.Id=B.UnitId
Join Building c
on B.ID=C.Subunitid
Join Room D
ON C.ID=D.buildingid
Join Orders E 
on E.Id=A.Id

So it looks like you are trying to have a table [Orders] which is going to link/ relate to potentially 4 other tables via the [AreaId] column.因此,您似乎正在尝试创建一个表 [Orders],该表将通过 [AreaId] 列链接/关联到潜在的 4 个其他表。

The problem is you haven't provided either a column to specify which table you are relating to for each particular order record.问题是您没有提供任何一列来指定您与每个特定订单记录相关的表。

Split the [Orders] table into 4 separate Orders tables with an order for each or provide a means, ie [UniteOrders], [SubUniteOrders], etc. Making each a one-unit to many orders relationship将 [Orders] 表拆分为 4 个单独的 Orders 表,每个表都有一个订单或提供一种方法,即 [UniteOrders]、[SubUniteOrders] 等。使每个表成为一个单位到多个订单的关系

Then if you want all orders, you could do a Union query to pull from all the tables然后,如果您想要所有订单,则可以执行 Union 查询以从所有表中提取

Another option would be to use GUIDs as the IDs and then you could store them in the [AreaId] and be certain to a high degree there would be no duplicates.另一种选择是使用 GUID 作为 ID,然后您可以将它们存储在 [AreaId] 中,并确保在很大程度上不会重复。

SQL Server doc on GUID type:关于 GUID 类型的 SQL Server 文档:

https://msdn.microsoft.com/en-us/library/ms187942.aspx https://msdn.microsoft.com/en-us/library/ms187942.aspx

Ill give this a shot with out having access to the data.我会在没有访问数据的情况下试一试。 Please let me know if this is what you're trying to accomplish.请让我知道这是否是您要完成的任务。

Select 
  U.ID
, U.NAME
, U.UniteName
, O_FROM_U.*
, SU.ID
, SU.NAME
, SU.UniteID
, O_FROM_SU.*
, B.ID
, B.Name
, B.SubUniteID
, O_FROM_B.*
, R.ID
, R.Name
, R.BuildingID
, O_FROM_R.*

FROM
Unites U
LEFT OUTER JOIN SubUnites SU        ON SU.UNITEID = U.ID
LEFT OUTER JOIN Building B          ON B.SubUniteID = SU.ID
LEFT OUTER JOIN Rooms R             ON R.BuildingID = B.ID

LEFT OUTER JOIN ORDERS O_FROM_U     ON U.ID = O_FROM_U.AreaID
LEFT OUTER JOIN ORDERS O_FROM_SU    ON SU.ID = O_FROM_SU.AreaID
LEFT OUTER JOIN ORDERS O_FROM_B     ON B.ID = O_FROM_B.AreaID
LEFT OUTER JOIN ORDERS O_FROM_R     ON R.ID = O_FROM_R.AreaID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM