Re: Left join syntax error - Mailing list pgsql-general

From Tom Lane
Subject Re: Left join syntax error
Date
Msg-id 2762582.1716045418@sss.pgh.pa.us
Whole thread Raw
In response to Re: Left join syntax error  (Erik Wienhold <ewie@ewie.name>)
List pgsql-general
Erik Wienhold <ewie@ewie.name> writes:
> But I wonder if the implicit cross join syntax ("FROM peoples, companies")
> should actually produce this error because the explicit cross join
> works:

>     SELECT p.lname, p.fname, p.job_title, p.company_nbr, p.email, c.company_name
>     FROM people as p
>         CROSS JOIN companies as c
>         LEFT JOIN companies ON c.company_nbr = p.company_nbr;

> But I'm not even sure if implicit and explicit cross join are
> semantically equivalent.

Well, they do the same thing, but JOIN binds tighter than comma.
So in one case you have effectively

    FROM people as p CROSS JOIN
         (companies as c LEFT JOIN companies ON c.company_nbr = p.company_nbr)

and "p" is not within the scope of the JOIN/ON clause.
The other way is effectively

    FROM (people as p CROSS JOIN companies as c)
         LEFT JOIN companies ON c.company_nbr = p.company_nbr;

which is syntactically legal, although it probably doesn't do
what you wanted.

If memory serves, MySQL got this basic syntactic detail wrong
for years, as a result of which there's (still) a tremendous amount
of confusion on the net about what is the syntactic precedence in
FROM clauses.

            regards, tom lane



pgsql-general by date:

Previous
From: Erik Wienhold
Date:
Subject: Re: Left join syntax error
Next
From: Rich Shepard
Date:
Subject: Re: Left join syntax error