基于公共列连接3个表

时间:2016-05-02 09:21:01

标签: sql-server join multiple-tables

我有四张桌子 - 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 |
+------------+-----------+------------+

我正在尝试从Data_DatetblData(数据存在的地方)tblData2加入与tblBase匹配的Base_Num。由于tblLookup中存在公共列,我需要连接所有四个表。

例如,Base_ID = 3Base_Num = 7890应从Data_Date获取tblData2,因为Base_ID (Banana)Base_Num (7890)都匹配。

我尝试过INNER JOIN,但是没有达到预期效果。

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

+---------+----------+-----------+------------+-------------------+
| 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        |
+---------+----------+-----------+------------+-------------------+

2 个答案:

答案 0 :(得分:1)

您可以尝试左加入

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>

答案 1 :(得分:0)

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