Re: joining from multiple tables

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Joseph Shraibman <jks(at)selectacast(dot)net>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: joining from multiple tables
Date: 2003-01-16 02:02:12
Message-ID: 20030115180057.B97388-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


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);

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2003-01-16 03:26:08 Re: show data from two tables together
Previous Message Ludwig Lim 2003-01-16 01:59:32 Re: RFC: A brief guide to nulls