Inner join & outer join; is the order of tables in from important?

前端 未结 5 2013

Why is the order of tables important when combining an outer & an inner join ? the following fails with postgres:

SELECT grp.number AS number,     
              


        
5条回答
  •  误落风尘
    2020-12-13 06:16

    I don't think anyone's quite nailed this, or explained it very well. You're combining 'old style' (theta) and 'new style' (ANSI) joins, which I strongly suspect are being grouped in ways you don't expect. Look at it this way:

    SELECT * FROM a, b JOIN c ON a.x = c.x
    

    is like saying

    SELECT * FROM a, (b JOIN c on a.x = c.x)
    

    where the bracketed thing represents a bunch of tables merged into one virtual table, to be joined on with a theta-join against 'a'. Obviously the 'a' table can't be part of the join as it's only being joined onto later. Reverse it, and you're doing

    SELECT * FROM b, (a JOIN c on a.x = c.x)
    

    which is perfectly understandable and so fine. I'm not sure why you're not using ANSI join syntax for all of it though, seems a little weird (and cruel to the person who has to maintain it!)

提交回复
热议问题