简体   繁体   English

将两个不同的表与公共列上的公共第三个表连接起来

[英]Joining two different tables with a common third table on a common column

Here are the tables这是表

Table: Status表:状态

ID, StatusDesc
1,  New
2,  Active
3,  Cancelled
4,  Complete

Table: Order (foreign key relationship with Status table above)表:订单(与上面Status表的外键关系)

ID, OrderNumber, StatusID
1,  1001 , 1
2,  1002,  1
3,  1003, 2
4,  1004, 3
5,  1500, 4

Table: LineItem(foreign key relationship with Order and Status tables above)表:LineItem(与上面的订单和状态表的外键关系)

ID, OrderNumber, LineItemNumber, StatusID
1,  1001 , 1, 1
2,  1001 , 2, 1
3,  1002 , 1, 2
4,  1002 , 2, 1
5,  1003 , 1, 2
6,  1004 , 1, 3
7,  1004 , 2, 4
8,  1500 , 1, 3

As you can see, the table Status holds the statuses common for both Order and LineItem tables.如您所见,Status 表保存了 Order 和 LineItem 表的共同状态。

I want to produce the result which will include columns like this, status description for both Order and LineItem:我想生成的结果将包括这样的列,订单和 LineItem 的状态描述:

OrderNumber, LineItemNumber, StatusDesc_Order, StatusDesc_LineItem

How to do this?这该怎么做?

You could join the Status table twice to achieve this:您可以两次加入 Status 表以实现此目的:

SELECT
           o.OrderNumber
         , li.LineItemNumber
         , orderStatus.StatusDesc    AS StatusDesc_Order
         , lineItemStatus.StatusDesc AS StatusDesc_LineItem
FROM       [LineItem] AS li
INNER JOIN [Status]   AS lineItemStatus ON li.StatusID = lineItemStatus.ID
INNER JOIN [Order]  AS o ON li.OrderNumber = o.OrderNumber
INNER JOIN [Status]   AS orderStatus ON o.StatusID = orderStatus.ID

I do suggest however you try and stay away from table names using reserved keywords like Order and Status, it also is good practice to explcitly add schema prefixes before the table names in the query (ie dbo.Status or another user defined schema).但是,我建议您尝试使用保留关键字(如 Order 和 Status)远离表名,在查询中的表名(即 dbo.Status 或其他用户定义的架构)之前显式添加架构前缀也是一种很好的做法。

If the required results really are that simple then just use a couple of sub-queries eg如果所需的结果真的那么简单,那么只需使用几个子查询,例如

-- SETUP TEST DATA

declare @Order table (id int, OrderNumber int, StatusId int)

insert into @Order (id, OrderNumber, StatusId)
  values (1, 1001, 1), (2, 1002, 1), (3, 1003, 2), (4, 1004, 3), (5, 1500, 4)

declare @LineItem table (id int, OrderNumber int, LineItemNumber int, StatusId int)

insert into @LineItem (id, OrderNumber, LineItemNumber, StatusId)
  values (1, 1001, 1, 1), (2, 1001, 2, 1), (3, 1002, 1, 2), (4, 1002, 2, 1), (5, 1003, 1, 2), (6, 1004, 1, 3), (7, 1004, 2, 4), (8, 1500, 2, 3)

declare @Status table (id int, StatusDesc varchar(32))

insert into @Status(id, StatusDesc)
  values (1,'New'), (2,'Active'), (3,'Cancelled'), (4,'Complete')

-- QUERY DATA

select LI.OrderNumber, LI.LineItemNumber
  , (select S.StatusDesc from @Status S where S.id = StatusId) [StatusDesc_Order]
  , (select S.StatusDesc from @Status S where S.id = (select O.StatusId from @Order O where O.OrderNumber = LI.OrderNumber)) [StatusDesc_LineItem]
from @LineItem LI
order by LI.OrderNumber, LI.LineItemNumber

Note: If you provide your sample data in this format in future questions you make your question much easier to answer.注意:如果您在以后的问题中以这种格式提供示例数据,您的问题就会更容易回答。

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

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