将多个表连接到桥表,同时保持数据的两面(包括NULL)

时间:2016-06-21 15:58:43

标签: sql sql-server join mapping

我有以下表格方案:

表A:

ID | Value
1    Apple
2    Orange
3    Banana

表B:

ID | Value
6    Sorbet
7    Ice Cream

表C(桥/映射表):

A_ID | B_ID
1      7

我想加入这些表,以便从表A和表B中获取所有数据,但也表明行之间存在映射(如果有)。理想的解决方案表如下所示:

A_ID | A_Value | Mapped? | B_ID | B_Value
1      Apple     Yes       7      Ice Cream
2      Orange    No        NULL   NULL
3      Banana    No        NULL   NULL
NULL   NULL      No        6      Sorbet

到目前为止,我已经尝试了许多使用LEFT JOIN和RIGHT JOIN的方法,但是无论如何,我都无法在连接结构中工作,因为它包含桥两侧的数据。

这个查询是我的想法可以工作但它仍然删除了许多未从我的结果映射的行:

select a.A_ID, a.Value, b.B_ID, b.Value
from
(Select * from TableA left join TableC on TableA.ID = TableC.A_ID) a
JOIN
(Select * from TableB left join TableC on TableB.ID = TableC.B_ID) b
on a.A_ID = b.A_ID and a.B_ID = b.B_ID

2 个答案:

答案 0 :(得分:1)

混合使用LEFT JOINFULL OUTER JOIN可以达到所需的输出:

SELECT  A.ID AS A_ID,
        A.Value AS A_Value,
        CASE WHEN C.A_ID IS NOT NULL THEN 'Yes'
             ELSE 'No'
        END AS Mapped,
        B.ID AS B_ID,
        B.Value AS B_Value
FROM    @TableA A
LEFT JOIN @TableC C ON C.A_ID = A.ID
FULL OUTER JOIN @TableB B ON B.ID = C.B_ID;

Working example

A_ID    A_Value Mapped  B_ID    B_Value
1       Apple   Yes     7       Ice Cream
2       Orange  No      NULL    NULL
3       Banana  No      NULL    NULL
NULL    NULL    No      6       Sorbet

答案 1 :(得分:0)

您需要2个完整的外连接:

   select 
        a.ID as A_ID, 
        a.Value as A_Value, 
        case when 
            (a.id=c.a_id and c.b_id=b.id) 
                then 'yes' 
                else 'no' 
        END as Mapped ,
        b.ID as B_ID, 
        b.Value as B_value
    from tableA a full outer join tableC c on a.id=c.a_id
    full outer join tableB b on c.b_id=b.id

,结果是

A_ID        A_Value Mapped B_ID        B_value
----------- ------- ------ ----------- ---------
1           Apple   yes    7           Ice Cream
2           Orange  no     NULL        NULL
3           Banana  no     NULL        NULL
NULL        NULL    no     6           Sorbet