Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 8EE916328DA for ; Mon, 22 Feb 2010 01:10:22 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 60124-05 for ; Mon, 22 Feb 2010 05:10:12 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-qy0-f171.google.com (mail-qy0-f171.google.com [209.85.221.171]) by mail.postgresql.org (Postfix) with ESMTP id EC378632817 for ; Mon, 22 Feb 2010 01:10:11 -0400 (AST) Received: by qyk1 with SMTP id 1so1352001qyk.0 for ; Sun, 21 Feb 2010 21:10:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=dyiW/pyHcD+VN2jMuPz7XYc6m9+qIyc7P3CytCd+Hq4=; b=OQwqT8vFeKXvqefFCyNSkHAN3WiAWvwhcWpPXh8lpSSzGd9/GjR6Lo8mE/LjYeix4Q 1lyiPar1gz5S/QXz30Uool4sTwuwnUjetBxn0vfIIvZD/QZbaxplAzzrTZ43f+iUk5zj thKEDm1ry32Mi/nwBWxGwpd4oNr0yHFgtKPqQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=Yy3ztPTL2tzmlvQ33L7lt7s4ugKeKI6ja42XYY00X6xKH4V4+LP5r9oAjVt/3mUDxI wIU2j2I60/Q55Rfv+NzHp3bE813meijC3TK+DcJlyk4tZf6O3HdrC95hGcT2zFZ1wc6H Z5+9maxFWPt1EZJ7YTB41cWs6RGwutnGKt/ww= Received: by 10.224.87.159 with SMTP id w31mr5609193qal.50.1266815411128; Sun, 21 Feb 2010 21:10:11 -0800 (PST) Received: from ?192.168.0.8? (rsargent.dsl.xmission.com [199.104.125.126]) by mx.google.com with ESMTPS id 23sm2327335qyk.15.2010.02.21.21.10.09 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sun, 21 Feb 2010 21:10:10 -0800 (PST) Message-ID: <4B8211B2.6020406@gmail.com> Date: Sun, 21 Feb 2010 22:10:10 -0700 From: Rob Sargent User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Gary Chambers CC: pgsql-sql@postgresql.org Subject: Re: Join Advice and Assistance References: <302670f21002211525r65caee9et396c374a3cd64b0d@mail.gmail.com> In-Reply-To: <302670f21002211525r65caee9et396c374a3cd64b0d@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201002/84 X-Sequence-Number: 33683 Gary Chambers wrote: > All, > > I've encountered a mental block due primarily to my inexperience with > moderately complex joins. Given the following three tables: > > Table "public.users" > Column | Type | Modifiers > -----------+------------------------+----------------------- > userid | bigint | not null > lname | character varying(64) | not null > fname | character varying(64) | not null > passwd | character varying(64) | not null > is_active | boolean | not null default true > > Table "public.user_emailaddrs" > Column | Type | Modifiers > -----------+------------------------+----------------------- > userid | bigint | not null > emailaddr | character varying(256) | not null > is_active | boolean | not null default true > > Table "public.usermetas" > Column | Type | Modifiers > ----------------+-----------------------------+------------------------ > userid | bigint | not null > startdate | timestamp without time zone | not null default now() > lastlogindate | timestamp without time zone | not null default now() > lastpwchange | timestamp without time zone | not null default now() > logincount | integer | not null default 1 > > users and usermetas is a one-to-one relationship. > users and user_emailaddrs is a one-to-many relationship. > > What is the best way to get these tables joined on userid and return > all emailaddr records from user_emailaddrs (e.g. if userid has three > (3) e-mail addresses in user_emailaddrs)? Is there any way to avoid > returning all fields in triplicate? Please feel free to criticize > where necessary. Thank you very much in advance. > > -- Gary Chambers > > /* Nothing fancy and nothing Microsoft! */ > > If you want records for user without email addresses you will need an outer join on user_emailaddrs /* untested */ select u.userId, u.lname, u.lastname ,m.startdate, a.emailaddr from users u join usermetas m on u.userid = m.userid left join user_emailaddrs a on m.userid = a.userid