Should I use the sql JOIN keyword for complex joins on multiple tables?

后端 未结 4 807
日久生厌
日久生厌 2021-02-03 12:42

I\'ve got the following request :

select * 
    from tbA A, tbB B, tbC C, tbD D
where 
    A.ID=B.ID and B.ID2= C.ID2 and A.ID=D.ID and C.ID3=D.ID3 and B.ID4=D.I         


        
4条回答
  •  孤独总比滥情好
    2021-02-03 13:02

    I find join syntax much easier to understand

    select *
    from tbA A
    inner join tbB B on a.id = b.id
    inner join tbC C on b.id2 = c.id2
    inner join tbD D on a.id = d.id and c.id3 = d.id3 and b.id4 = d.id4
    where A.Foo='Foo'
    

    Now you can clearly see how data are joined together and that it is not a very complicated join altogether.

    BTW, the database design in your example strongly smells of lacking normalization. Usually you should have either one table joining to many (a join b on a.bid = b.bid join c on a.cid= c.cid) or a chain (a join b on a.bid = b.bid join c on b.cid = c.cid).

    EDIT. Added optional keyword INNER which does not change result, but makes it more clear.

提交回复
热议问题