Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id F1DC0632739 for ; Wed, 4 Aug 2010 08:11:49 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 04443-10 for ; Wed, 4 Aug 2010 11:11:41 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from jupiter.marktest.pt (delta.marktest.pt [80.251.174.116]) by mail.postgresql.org (Postfix) with ESMTP id 7F254632712 for ; Wed, 4 Aug 2010 08:11:42 -0300 (ADT) Received: from Proteu (proteu.marktest.pt [10.61.90.54]) by jupiter.marktest.pt with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id P5AX7021; Wed, 4 Aug 2010 12:11:36 +0100 Message-ID: <4283335591EA4532BCE90938FCF5136F@marktestcr.marktest.pt> From: "Oliveiros d'Azevedo Cristina" To: "Jedrin" , References: <433c09b3-b89e-4c66-9849-944d2d97e0e8@f42g2000yqn.googlegroups.com> Subject: Re: join table problem Date: Wed, 4 Aug 2010 12:11:35 +0100 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.5512 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.229 tagged_above=-5 required=5 tests=BAYES_50=0.8, STOX_REPLY_TYPE=0.439, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: * X-Archive-Number: 201008/11 X-Sequence-Number: 34451 Howdy! > At any rate, say I have 3 tables: > > table plant > id:integer > name string > > > table seed_supplier > id: integer > company_name: string > > table plant_seed_supplier > plant_id > seed_supplier_id > > plant_seed_supplier is a join table that supports a many to many > relationship between the plant table and the seed supplier table since > any plant may have multiple seed suppliers and any seed supplier can > sell seeds for many different plants. > > I would like to be able to write a single query that basically does > this: > > It will show only one distinct row for each plant in the database. It > will show either just one seed_supplier company name associated with > that plant, or if there is no supplier associated with that plant it > will show the plant in a row with that field empty. > You do not specify what is the seed_supplier company name that should appear in the case there is more than one Give this (untested) query a try SELECT name,MIN(company_name) FROM plant a JOIN plant_seed_supplier associative ON plant_id = a.id JOIN seed_supplier b ON b.id = supplier_id GROUP BY name Best, Oliveiros