SQL Server Left Join With 'Or' Operator

前端 未结 4 1750
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:42

    Given how little of the query is being exposed; a very rough rule of thumb is to replace an Or with a Union to avoid table scanning.

    Select..
    LEFT JOIN Child c ON c.ParentAId = a.ParentAId 
    union
    Select..
    left Join Child c ON c.ParentBId = b.ParentBId
    

提交回复
热议问题