pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: Joseph Shraibman <jks@selectacast.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: joining from multiple tables
Date: Wed, 15 Jan 2003 18:02:12 -0800 (PST)
Message-ID: <20030115180057.B97388-100000@megazone23.bigpanda.com> (raw)
In-Reply-To: <3E260E41.30107@selectacast.net>


On Wed, 15 Jan 2003, Joseph Shraibman wrote:

> select distinct n.thekey, n.val, t.txt  FROM  num_tab n LEFT JOIN txt_tab t ON n.thekey =
> t.thekey
> WHERE  n.thekey < 5 AND t.class = class_tab.tkey AND n.class = class_tab.class;
> produces:

Note that the above uses the non-standard postgres behavior of adding from
clauses, it's not technically valid SQL.

>   thekey | val | txt
> --------+-----+------
>        2 |   2 | two
>        4 |   4 | four
> ... which is not what we want, because 1,3, and 5 aren't included, but:
>
> select distinct n.thekey, n.val, t.txt  FROM  num_tab n LEFT JOIN txt_tab t ON n.thekey =
> t.thekey  AND t.class = class_tab.tkey AND n.class = class_tab.class
> WHERE  n.thekey < 5;
>   produces:
> NOTICE:  Adding missing FROM-clause entry for table "class_tab"
> ERROR:  JOIN/ON clause refers to "class_tab", which is not part of JOIN
>
> So how do I do this?

I think you want something like:

select distinct n.thekey, n.val, t.txt  FROM class_tab JOIN num_tab n
using (class) LEFT JOIN txt_tab t on (t.thekey=n.thekey and t.class =
class_tab.tkey);




view thread (2+ messages)

Message-ID: <20030115180057.B97388-100000@megazone23.bigpanda.com>
Permalink:  ../20030115180057.B97388-100000@megazone23.bigpanda.com/
Also on:    postgresql.org/message-id/20030115180057.B97388-100000@megazone23.bigpanda.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: sszabo@megazone23.bigpanda.com, jks@selectacast.net
  Subject: Re: joining from multiple tables
  In-Reply-To: <20030115180057.B97388-100000@megazone23.bigpanda.com>

* 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