将两个查询连接到共享表

时间:2015-08-08 01:17:16

标签: sql sql-server

我一直在努力将两个查询的结果通过公共表上的连接组合在一起的概念,我希望我能获得一些帮助。以下是表格的粗略指南:

dbo.Asset (not returned in the SELECT statement, used for joins only)
 - dbo.Asset.AssetID
 - dbo.Asset.CatalogueID

dbo.WorkOrder
 - WorkOrderID
 - AssetID
 - WorkOrderNumber

dbo.WorkOrderSpare
 - WorkOrderID
 - WorkOrderSpareID
 - WorkOrderSpareDescription
 - ActualQuantity
 - CreatedDateTime

dbo.Catalogue
 - CatalogueID (PK)
 - CatalogueNumber
 - CatalogueDescription
 - CatalogueGroupID

dbo.CatalogueGroup
 - CatalogueGroupID
 - CatalogueGroupNumber

第一次查询:

SELECT CatalogueGroup.CatalogueGroupName,
    Catalogue.CatalogueNumber,
    Catalogue.CatalogueDescription,
    Catalogue.CatalogueID,
    Asset.IsActive
FROM CatalogueGroup
INNER JOIN Catalogue 
ON CatalogueGroup.CatalogueGroupID = Catalogue.CatalogueGroupID
INNER JOIN Asset 
ON Catalogue.CatalogueID = Asset.CatalogueID

第二次查询:

SELECT WorkOrder.WorkOrderNumber,
    WorkOrderSpare.WorkOrderSpareDescription,
    WorkOrderSpare.ActualQuantity,
    WorkOrderSpare.CreatedDateTime
FROM WorkOrder
INNER JOIN WorkOrderSpare
ON WorkOrder.WorkOrderID = WorkOrderSpare.WorkOrderID

现在,我可以通过将Asset / Catalog / CatalogueGroup连接在一起,然后将Asset.AssetID加入WorkOrder.AssetID,在Access(WYSIWYG)中轻松完成上述操作。我似乎无法通过原始代码获得任何类似的工作,我认为我的逻辑对于连接是正确的(INNER JOIN对两者的结果)但是我是新手。

任何帮助都会非常感激,任何有关我可以进一步阅读此类问题的指示都会很棒。

编辑:这是我试图使用无济于事,我还应该提到我试图在ms-sql中执行此操作,而不是Access(尝试远离拖放):

SELECT CatalogueGroup.CatalogueGroupName,
    Catalogue.CatalogueNumber,
    Catalogue.CatalogueDescription,
    Catalogue.CatalogueID,
    Asset.IsActive,
    WorkOrderSpare.WorkOrderSpareDescription,
    WorkOrderSpare.ActualQuantity,
    WorkOrderSpare.CreatedDateTime,
    WorkOrder.WorkOrderNumber
FROM (((CatalogueGroup
INNER JOIN Catalogue 
ON CatalogueGroup.CatalogueGroupID = Catalogue.CatalogueGroupID)
INNER JOIN Asset ON Catalogue.CatalogueID = Asset.CatalogueID)
INNER JOIN WorkOrderSpare 
ON WorkOrder.WorkOrderID = WorkOrderSpare.WorkOrderID)
INNER JOIN WorkOrder ON Asset.AssetID = WorkOrder.AssetID

3 个答案:

答案 0 :(得分:0)

我觉得你很亲密。作为一种稍微不同的加入方法,请尝试以下方法:

SELECT CatalogueGroup.CatalogueGroupName,
  Catalogue.CatalogueNumber,
  Catalogue.CatalogueDescription,
  Catalogue.CatalogueID,
  Asset.IsActive,
  WorkOrderSpare.WorkOrderSpareDescription,
  WorkOrderSpare.ActualQuantity,
  WorkOrderSpare.CreatedDateTime,
  WorkOrder.WorkOrderNumber
FROM 
  CatalogueGroup, Catalogue, Asset, WorkOrder, WorkOrderSpare
WHERE CatalogueGroup.CatalogueGroupID = Catalogue.CatalogueGroupID
  and Catalogue.CatalogueID = Asset.CatalogueID
  and Asset.AssetID = WorkOrder.AssetID
  and WorkOrder.WorkOrderID = WorkOrderSpare.WorkOrderID

它看起来应该有效,但没有数据,很难知道简单连接是否是你想要的。 (这是个人偏好的问题,是否使用and子句而不是内连接语法。在样式首选项上,如果支持,我喜欢表别名,所以FROM CatalogueGroup cg例如,以便您可以参考到cg.CatalogueGroupID等,而不是每次都写出完整的表名。)

答案 1 :(得分:0)

确定。所以您在评论中注意到的错误

  

无法绑定多部分标识符“WorkOrder.WorkOrderID”

通常是在您拥有表的别名而不是在JOIN中使用别名时使用表名或使用错误的列名或表名。

理想情况下,在SQL Server中,您的查询应该如下所示

 SELECT 
    cg.CatalogueGroupName, 
    c.CatalogueNumber, 
    c.CatalogueDescription, 
    c.CatalogueID, 
    A.IsActive,
    Wo.WorkOrderNumber,
    WoS.WorkOrderSpareDescription,
    WoS.ActualQuantity,
    WoS.CreatedDateTime
 FROM CatalogueGroup cg 
    INNER JOIN Catalogue c ON cg.CatalogueGroupID = c.CatalogueGroupID
    INNER JOIN Asset A ON c.CatalogueID = A.CatalogueID
    INNER JOIN WorkOrder Wo ON Wo.AssetID= A.AssetID
    INNER JOIN WorkOrderSpare WoS ON Wo.WorkOrderID = WoS.WorkOrderID

答案 2 :(得分:0)

想想我看到了这个问题。假设连接本身是正确的(即,您的列确实相互关联),您的连接顺序有点偏离 - 当您将WorkOrder加入WorkOrderSpare时,这两个表中的任何一个都不会与您已识别出的任何表相关联直到查询中的那一点。可以把它想象为与目前为止的连接链分开加入两个表 - 这几乎就像做两个单独的连接查询一样。如果你切换最后两个它应该工作,那么WorkOrder将加入到Asset(你已经定义),然后你可以将WorkOrderSpare加入WorkOrder。我也冒昧地删除了连接上的括号,这是一个Access的东西。

SELECT CatalogueGroup.CatalogueGroupName,
    Catalogue.CatalogueNumber,
    Catalogue.CatalogueDescription,
    Catalogue.CatalogueID,
    Asset.IsActive,
    WorkOrderSpare.WorkOrderSpareDescription,
    WorkOrderSpare.ActualQuantity,
    WorkOrderSpare.CreatedDateTime,
    WorkOrder.WorkOrderNumber
FROM CatalogueGroup
INNER JOIN Catalogue ON CatalogueGroup.CatalogueGroupID = Catalogue.CatalogueGroupID
INNER JOIN Asset ON Catalogue.CatalogueID = Asset.CatalogueID
INNER JOIN WorkOrder ON Asset.AssetID = WorkOrder.AssetID
INNER JOIN WorkOrderSpare ON WorkOrder.WorkOrderID = WorkOrderSpare.WorkOrderID