Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 3B068475BA1 for ; Wed, 15 Jan 2003 20:43:27 -0500 (EST) Received: from p1.selectacast.net (p1.selectacast.net [199.107.233.195]) by postgresql.org (Postfix) with ESMTP id 8BA5D475843 for ; Wed, 15 Jan 2003 20:43:26 -0500 (EST) Received: from selectacast.net (IDENT:jks@d1 [64.7.9.58]) by p1.selectacast.net (8.9.3/8.9.3) with ESMTP id UAA09836 for ; Wed, 15 Jan 2003 20:43:29 -0500 Message-ID: <3E260E41.30107@selectacast.net> Date: Wed, 15 Jan 2003 20:43:29 -0500 From: Joseph Shraibman User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021130 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: joining from multiple tables Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/242 X-Sequence-Number: 11107 I have a table I want to join on, but the conditions that restrict it span more than one table. For example: create table num_tab (thekey int primary key, val int, class char); create table class_tab (class char primary key, tkey int); create table txt_tab (thekey int primary key, class int, txt text); insert into num_tab values (1, 1, 'o'); insert into num_tab values (2, 2, 'e'); insert into num_tab values (3, 3, 'o'); insert into num_tab values (4, 4, 'e'); insert into num_tab values (5, 5, 'o'); insert into num_tab values (6, 6, 'e'); insert into class_tab values('o', 1); insert into class_tab values('e', 2); insert into txt_tab values (2, 2,'two'); insert into txt_tab values (4, 2,'four'); insert into txt_tab values (6, 2,'six'); 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: 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?