基于公共列连接 3 个表

Joining 3 tables based on common columns

我有四个 table - tblBasetblLookuptblDatatblData2

tblBase

+---------+----------+-----------+------------+
| Base_ID | Base_Num | Base_Type | Base_Date  |
+---------+----------+-----------+------------+
|       1 |     1234 | ABC       | 01/05/2016 |
|       2 |     3456 | DEF       | 02/05/2016 |
|       3 |     7890 | GHI       | 03/05/2016 |
+---------+----------+-----------+------------+

tblLookup

+-----------+-------------+
| Lookup_ID | Lookup_Name |
+-----------+-------------+
|         1 | Apple       |
|         2 | Orange      |
|         3 | Banana      |
+-----------+-------------+

tblData

+-----------+----------+------------+
| Data_Name | Data_Num | Data_Date  |
+-----------+----------+------------+
| Apple     |     1234 | 02/05/2016 |
| Orange    |     3456 | 03/05/2016 |
| Guava     |     5937 | 04/05/2016 |
+-----------+----------+------------+

tblData2

+------------+-----------+------------+
| Data2_Name | Data2_Num | Data2_Date |
+------------+-----------+------------+
| Grapes     |      3953 | 02/05/2016 |
| Orange     |      3456 | 03/05/2016 |
| Banana     |      7890 | 04/05/2016 |
| Banana     |      1473 | 07/05/2016 |
+------------+-----------+------------+

我正在尝试从 tblDatatblData2(数据存在的地方)获取 Data_DateBase_Num 匹配的 tblBase 连接。由于 tblLookup 中存在公共列,我需要加入所有四个 table。

例如,Base_ID = 3Base_Num = 7890 应该从 tblData2 中选择 Data_Date,因为 Base_ID (Banana)Base_Num (7890) 都匹配。

我试过INNER JOIN,但没有得到想要的结果。

我正在寻找这样的结果 table:

+---------+----------+-----------+------------+-------------------+
| Base_ID | Base_Num | Base_Type | Base_Date  | Desired_Data_Date |
+---------+----------+-----------+------------+-------------------+
|       1 |     1234 | ABC       | 01/05/2016 | 02/05/2016        |
|       2 |     3456 | DEF       | 02/05/2016 | 03/05/2016        |
|       3 |     7890 | GHI       | 03/05/2016 | 04/05/2016        |
+---------+----------+-----------+------------+-------------------+

您可以尝试左加入

SELECT B.Base_ID, B.Base_Num , B.Base_Type, B.Base_Date,
D1.Data_Name AS Data1, D1.Data_Date AS DESIRED_DATE1  
D2.Data2_Name AS Data2, D2.Data2_Date  AS DESIRED_ 

FROM tblBase B

JOIN tblLookup L ON L.Lookup_ID=B.Base_ID
LEFT JOIN tblData D1 ON D1.Data_Num = B.Base_Num 
LEFT JOIN tblData2 D2 ON D2.Data2_Num = B.Base_Num 
WHERE <Condition>
declare @tblbase table (Base_ID int, Base_Num int, Base_Type varchar(3), Base_Date  varchar(10))
Insert into @tblbase
values 
(       1 ,     1234 , 'ABC', '01/05/2016'),
(       2  ,    3456 , 'DEF', '02/05/2016'),
(       3  ,    7890 , 'GHI', '03/05/2016')

declare @tblLookup table (Lookup_ID int, Lookup_Name varchar(10))
insert into @tblLookup      
values
(         1 , 'Apple' ),
(         2 , 'Orange'),
(         3 , 'Banana') 

declare @tbldata table (Data_Name varchar(10), Data_Num int, Data_Date varchar(10))
Insert into @tbldata
values
( 'Apple'  ,     1234 , '02/05/2016'),
( 'Orange' ,     3456 , '03/05/2016'),
( 'Guava'  ,     5937 , '04/05/2016')

declare @tbldata2 table (Data_Name varchar(10), Data_Num int, Data_Date varchar(10))
Insert into @tbldata2
values
( 'Grapes',           3953 , '02/05/2016'),
( 'Orange' ,          3456 , '03/05/2016'),
( 'Banana'  ,         7890 , '04/05/2016'),
( 'Banana'   ,        1473 , '07/05/2016')

/*
Expected result
+---------+----------+-----------+------------+-------------------+
| Base_ID | Base_Num | Base_Type | Base_Date  | Desired_Data_Date |
+---------+----------+-----------+------------+-------------------+
|       1 |     1234 | ABC       | 01/05/2016 | 02/05/2016        |
|       2 |     3456 | DEF       | 02/05/2016 | 03/05/2016        |
|       3 |     7890 | GHI       | 03/05/2016 | 04/05/2016        |
+---------+----------+-----------+------------+-------------------+
*/
select bt.*,u.data_date as Desired_data_date
from    @tblbase bt
join    @tblLookup  lu on lu.lookup_id = bt.base_id
join
(select t1.*    from @tbldata t1
union
select  t2.*    from @tbldata2 t2
)  u
on      u.data_name = lu.Lookup_Name
where   u.Data_Num = bt.Base_Num
order   by bt.Base_Date