Received: from mps4.plala.or.jp (mpsb-nat02.plala.or.jp [202.212.114.145]) by postgresql.org (Postfix) with ESMTP id 82883475AC0 for ; Thu, 4 Apr 2002 09:59:48 -0500 (EST) Received: from vip333 ([218.47.117.80]) by mps4.plala.or.jp with ESMTP id <20020404145944.IRMH2133.mps4.plala.or.jp@vip333>; Thu, 4 Apr 2002 23:59:44 +0900 Date: Fri, 05 Apr 2002 00:01:46 +0900 From: Masaru Sugawara To: Michael Adler Subject: Re: FULL JOIN with 3 or more tables Cc: pgsql-sql@postgresql.org In-Reply-To: References: Errors-To: rk73@sea.plala.or.jp Message-Id: <20020405000052.5991.RK73@sea.plala.or.jp> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Becky! ver. 2.00.08 X-Archive-Number: 200204/53 X-Sequence-Number: 7318 On Wed, 3 Apr 2002 15:58:55 -0500 (EST) Michael Adler wrote: > I can do full joins just fine on two tables at a time: > > FROM t1 FULL JOIN t2 ON (t1.id = t2.id) > > Now, how would I full joint in a third table? > > FROM t1 FULL JOIN t2 ON (t1.id = t2.id) FULL JOIN t3 ON t1.id = t3.id > > That previous line would not join together two rows from t2 and t3. This topic is the same as the previous discussions(see the following URL). It seems to be still impossible to merge all the tables by that query. http://groups.google.com/groups?hl=en&threadm=3507.1006111223%40sss.pgh.pa.us&rnum=1&prev=/groups%3Fq%3DCOALESCE(t1.name,t2.name)%26hl%3Den%26selm%3D3507.1006111223%2540sss.pgh.pa.us%26rnum%3D1 But, if using a COALESCE(), you'll be able to merge. t1.id: 1,2, 4,5 t2.id: 1, 3,4 t3.id: 2,3, 5,6 SELECT t1.id AS id1, t2.id AS id2, t3.id AS id3 FROM t1 FULL JOIN t2 ON (t1.id = t2.id) FULL JOIN t3 ON (t1.id = t3.id); id1 | id2 | id3 -----+-----+----- 1 | 1 | 2 | | 2 | 3 | | | 3 4 | 4 | 5 | | 5 | | 6 (7 rows) explain analyze SELECT t.id1, t.id2 , t3.id AS id3 FROM (SELECT COALESCE(t1.id, t2.id) AS id12, t1.id AS id1, t2.id AS id2 FROM t1 FULL JOIN t2 ON (t1.id = t2.id) ) AS t FULL JOIN t3 ON (t.id12 = t3.id); id1 | id2 | id3 -----+-----+----- 1 | 1 | 2 | | 2 | 3 | 3 <-- being merged 4 | 4 | 5 | | 5 | | 6 (6 rows) Regards, Masaru Sugawara