pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Mark Jeffcoat <jeffcoat@alumni.rice.edu>
Cc: pgsql-general@postgresql.org
Subject: Re: Join condition parsing puzzle
Date: Thu, 23 Aug 2018 17:51:03 -0400
Message-ID: <12727.1535061063@sss.pgh.pa.us> (raw)
In-Reply-To: <CAPN82eQkDCtwG7E404DD3SqZdZoKp_xRSv5=P5+rMkfTGxxFTg@mail.gmail.com>
References: <CAPN82eQkDCtwG7E404DD3SqZdZoKp_xRSv5=P5+rMkfTGxxFTg@mail.gmail.com>

Mark Jeffcoat <jeffcoat@alumni.rice.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




view thread (3+ messages)  latest in thread

Message-ID: <12727.1535061063@sss.pgh.pa.us>
Permalink:  ../12727.1535061063@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/12727.1535061063@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, jeffcoat@alumni.rice.edu
  Subject: Re: Join condition parsing puzzle
  In-Reply-To: <12727.1535061063@sss.pgh.pa.us>
  References: <CAPN82eQkDCtwG7E404DD3SqZdZoKp_xRSv5=P5+rMkfTGxxFTg@mail.gmail.com> <12727.1535061063@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox