Re: Join condition parsing puzzle

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Mark Jeffcoat <jeffcoat(at)alumni(dot)rice(dot)edu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Join condition parsing puzzle
Date: 2018-08-23 21:51:03
Message-ID: 12727.1535061063@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Mark Jeffcoat <jeffcoat(at)alumni(dot)rice(dot)edu> writes:
> SELECT *
> FROM relation_a
> LEFT JOIN relation_b
> JOIN relation_c
> ON (relation_c.id_p = relation_b.id_p)
> ON (relation_a.id_c = relation_b.id_c AND relation_a.id_v = relation_b.id_v);

> I would have claimed before seeing this example that it wasn't even
> grammatical; I thought the only legal place to write the ON clause was
> immediately after the JOIN. Apparently not.

> How should I read this query? I'd appreciate any help understanding this.

You read it as

SELECT *
FROM
relation_a
LEFT JOIN (relation_b
JOIN relation_c
ON (relation_c.id_p = relation_b.id_p))
ON (relation_a.id_c = relation_b.id_c AND relation_a.id_v = relation_b.id_v);

There's no other valid way to parenthesize it, so that's what
the parser does.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Laurenz Albe 2018-08-24 00:46:38 Re: Is my text_pattern_ops index working for a LIKE prefix search with column reference?
Previous Message Nikolay Samokhvalov 2018-08-23 19:40:15 Re: Upgrade/Downgrade