SQL Server Left Join With 'Or' Operator

前端 未结 4 1718
Happy的楠姐
Happy的楠姐 2021-01-03 19:21

I have a four tables, TopLevelParent, two mid level tables MidParentA and MidParentB, and a Child table which can have a parent of MidParentA or MidParentB (One or the other

4条回答
  •  感情败类
    2021-01-03 19:49

    another way to write it:

    LEFT JOIN Child c ON c.ParentAId = COALESCE(a.ParentAId, b.ParentBId)

    Edit

    One possible approach is querying first the MidParentA and then the MidParentB and then UNION the results:

    SELECT tlp.*,
           a.MidParentAId,
           null MidParentBId,
           c.ChildId
    FROM TopLevelParent tlp
    LEFT JOIN MidParentA a ON tlp.TopLevelPatientId = a.TopLevelPatientId
    LEFT JOIN Child c ON c.MidParentAId = a.MidParentAId 
    UNION
    SELECT tlp.*,
           null MidParentAId,
           b.MidParentBId,
           c.ChildId
    FROM TopLevelParent tlp
    LEFT JOIN MidParentB b ON tlp.TopLevelPatientId = b.TopLevelPatientId
    LEFT JOIN Child c ON c.MidParentBId = b.MidParentBId 
    

    A demo in SQLFiddle

提交回复
热议问题