Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id C814C4774E2 for ; Wed, 15 Jan 2003 21:21:32 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id 26751477260 for ; Wed, 15 Jan 2003 21:02:09 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 9CAA9D60D; Wed, 15 Jan 2003 18:02:12 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 9257B5C02; Wed, 15 Jan 2003 18:02:12 -0800 (PST) Date: Wed, 15 Jan 2003 18:02:12 -0800 (PST) From: Stephan Szabo To: Joseph Shraibman Cc: Subject: Re: joining from multiple tables In-Reply-To: <3E260E41.30107@selectacast.net> Message-ID: <20030115180057.B97388-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/245 X-Sequence-Number: 11110 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);