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

后端 未结 4 865
日久生厌
日久生厌 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:10

    It's a matter of taste, but I like the JOIN keyword better. It makes the logic clearer and is more consistent with the LEFT OUTER JOIN syntax that goes with it. Note that you can also use INNER JOIN which is synonymous with JOIN.

    The syntax is

       a JOIN b
        ON expression relating b to all of the tables before
    

    b can be a join itself. For inner joins it doesn't matter, but for outer you can control the order of the joins like this:

    select * from
       a left join
          d join c
          on d.i = c.i
       on a.k = d.k 
    

    Here a is left-joined to the inner join between d and c.

    Here is your query:

    select * 
        from tbA A
        join tbB B on A.ID = B.ID
        join tbC C on B.ID2 = C.ID2
        join tbD D on A.ID = D.ID and C.ID3 = D.ID3 and B.ID4 = D.ID4
    where 
        A.Foo='Foo'
    

提交回复
热议问题