Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 686726337DD for ; Tue, 16 Feb 2010 17:43:22 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 86602-04-5 for ; Tue, 16 Feb 2010 21:43:07 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sargon.lncsa.com (mail.lncsa.com [213.215.28.11]) by mail.postgresql.org (Postfix) with ESMTP id 733E763390E for ; Tue, 16 Feb 2010 17:43:07 -0400 (AST) Received: from localhost (localhost.localdomain [127.0.0.1]) by sargon.lncsa.com (Postfix) with ESMTP id 20B39300E35B for ; Tue, 16 Feb 2010 22:43:06 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at lncsa.com Received: from sargon.lncsa.com ([127.0.0.1]) by localhost (sargon.lncsa.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id DaJAdIUKEBg3 for ; Tue, 16 Feb 2010 22:43:06 +0100 (CET) Received: from zenon.apartia.fr (zenon.apartia.fr [10.0.3.1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "zenon.apartia.fr", Issuer "ca.apartia.fr" (verified OK)) by sargon.lncsa.com (Postfix) with ESMTP id F421D300E355 for ; Tue, 16 Feb 2010 22:43:05 +0100 (CET) Received: from delos.apartia.fr (delos.apartia.fr [10.0.3.107]) by zenon.apartia.fr (Postfix) with ESMTP id 9EACE5B93F8F2 for ; Tue, 16 Feb 2010 22:43:05 +0100 (CET) Received: by delos.apartia.fr (Postfix, from userid 1000) id 93960200177D5; Tue, 16 Feb 2010 22:43:05 +0100 (CET) Date: Tue, 16 Feb 2010 22:43:05 +0100 From: Louis-David Mitterrand To: pgsql-sql@postgresql.org Subject: Re: UNION or LEFT JOIN? Message-ID: <20100216214305.GA22807@apartia.fr> Mail-Followup-To: pgsql-sql@postgresql.org References: <20100216140814.GA22577@apartia.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.515 tagged_above=-10 required=5 tests=AWL=-0.775, BAYES_20=-0.74 X-Spam-Level: X-Archive-Number: 201002/61 X-Sequence-Number: 33660 On Tue, Feb 16, 2010 at 09:38:19PM +0000, Tim Landscheidt wrote: > Louis-David Mitterrand wrote: > > > Here is the basic schema: > > > -------------------------->id_ship>----------------------- > > | | > > [SHIP]->id_ship->[CABIN]->id_cabin->[PRICE]<-id_cruise<-[CRUISE] > > > It's a database of cruise prices. > > > Each 'price' object has a reference to 'cabin' and 'cruise' > > > 'cabin' belongs to a 'ship', so does 'cruise' > > > I'm trying to select all cabins of cruise N°1 with prices OR nothing if > > there is no price (meaning cabin not available). I want all cabins > > listed, price or no price. > > > Also when doing the query I don't have the id_ship, only the id_cruise. > > > What is the best way of doing it? UNION or LEFT JOIN? I tried the latter > > without success and am unsure on how do do the former. > > Was does "without success" mean? The objective seems to be > straight-forward: > > - Select all cabins that belong to the ship that belongs to > the cruise id_cruise. > - Left join that with the prices of the cruise id_cruise. Definitely the way to go. As the real schema is quite a bit more complicated I was struggling with very long statements, but finally succeded with a simple left join. Thanks,