X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id E1643D1EE05 for ; Sun, 2 May 2004 13:45:53 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 43554-07 for ; Sun, 2 May 2004 13:45:33 -0300 (ADT) Received: from mailhub2.sghms.ac.uk (firewall.sghms.ac.uk [194.82.50.2]) by svr1.postgresql.org (Postfix) with ESMTP id 372CCD1E9C1 for ; Sun, 2 May 2004 13:45:32 -0300 (ADT) Received: from [194.82.51.24] (helo=imail) by mailhub2.sghms.ac.uk with esmtp (Exim 4.24) id 1BKK5l-0002V2-U2; Sun, 02 May 2004 17:45:25 +0100 Received: from [172.16.20.3] (mrc1-003.sghms.ac.uk [172.16.20.3]) by imail.sghms.ac.uk (iPlanet Messaging Server 5.2 Patch 1 (built Aug 19 2002)) with ESMTPA id <0HX300LRBIJPH3@imail.sghms.ac.uk>; Sun, 02 May 2004 17:45:25 +0100 (BST) Date: Sun, 02 May 2004 17:44:36 +0100 From: Adam Witney Subject: Re: double left outer join on the same table In-reply-to: <4095207C.8080009@numerixtechnology.de> To: mailreg@numerixtechnology.de, pgsql-sql Message-id: MIME-version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT User-Agent: Microsoft-Entourage/10.1.1.2418 X-MailScanner-Information: Please contact sysadmin at sghms.ac.uk for more information X-MailScanner-MH2: No Virues detected X-MailScanner-SpamCheck: not spam, SpamAssassin (score=-4.4, required 5, APARENTLY_FROM_MYSELF 1.00, BAYES_00 -4.90, FROM_SGHMS -1.00, MY_NO_QU 0.50, UPPERCASE_25_50 0.00) X-MailScanner-From: awitney@sghms.ac.uk X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.3 tagged_above=0.0 required=5.0 tests=UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200405/5 X-Sequence-Number: 17719 On 2/5/04 5:23 pm, "T E Schmitz" wrote: > Hello, > > I have two tables SECTION and BRAND. SECTION is related to BRAND via two > foreign keys. I would like to select ALL SECTIONs whether the FKs are > null or not and fetch the BRAND attributes in one SQL statement. In > other words I need a double outer join. > > Is this possible at all? > > The following doesn't work for two reasons: > a) Table name "brand" specified more than once. > b) how would I specify the same output columns twice? > > SELECT > SECTION.SECTION_PK, > SECTION.SECTION_NAME, > SECTION.BRAND_1_FK, > SECTION.BRAND_2_FK, > BRAND.BRAND_PK, > BRAND.BRAND_NAME > > FROM SECTION > left outer join BRAND on BRAND_PK =BRAND_1_FK > left outer join BRAND on BRAND_PK =BRAND_2_FK > I don't know if this will solve your specific problem, but you can use the same table twice in the same query by using aliases, something like this (untested of course) SELECT SECTION.SECTION_PK, SECTION.SECTION_NAME, SECTION.BRAND_1_FK, SECTION.BRAND_2_FK, a.BRAND_PK, a.BRAND_NAME b.BRAND_PK, b.BRAND_NAME FROM SECTION left outer join BRAND a on BRAND_PK =BRAND_1_FK left outer join BRAND b on BRAND_PK =BRAND_2_FK -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.