pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephen Belcher <sycobuny@malkier.net>
To: Rob Sargent <robjsargent@gmail.com>
Cc: Gary Chambers <gwchamb@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Join Advice and Assistance
Date: Mon, 22 Feb 2010 12:15:06 -0500
Message-ID: <b4651e821002220915w13f81ed4p26fb0a7126ce1fff@mail.gmail.com> (raw)
In-Reply-To: <4B82B960.3060901@gmail.com>
References: <302670f21002211525r65caee9et396c374a3cd64b0d@mail.gmail.com>
	<4B8211B2.6020406@gmail.com>
	<302670f21002220751h6cc363fiad449314d961fcc0@mail.gmail.com>
	<4B82B960.3060901@gmail.com>

To expand on Rob's reply:

If you want to return a single row for each user, regardless of the number
of email addresses, you might use ARRAY() with a subquery, eg (haven't
tested this to make sure it completely works):

SELECT u.*, um.*, ARRAY(SELECT emailaddr FROM user_emailaddrs em WHERE
em.userid = u.userid AND em.is_active) AS email_addresses
FROM users u INNER JOIN usermetas um ON u.userid = um.userid;

Of course, this will return the addresses as a character varying[], with
output like {user@domain.tld,user@domain.tld}, and would require some minor
contortions to present it to users cleanly. The array_to_string function may
help you make it easier to display the results.

http://www.postgresql.org/docs/8.4/static/functions-array.html#ARRAY-FUNCTIONS-TABLE

Hope this helps,
--Stephen Belcher

On Mon, Feb 22, 2010 at 12:05 PM, Rob Sargent <robjsargent@gmail.com> wrote:

> My mistake.  Should answer these things late at night.
>
> I think you will find that arrays will be your friend[s]
>
>
> On 02/22/2010 08:51 AM, Gary Chambers wrote:
>
>> Rob,
>>
>> Thanks for the reply...
>>
>>  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
>>>
>>
>> My question was related more toward eliminating the query returning a
>> record for each record in the one-to-many table.  I see now that I'm
>> going to have to aggregate the e-mail addresses in order to return a
>> single row.  Thanks again.
>>
>> -- Gary Chambers
>>
>> /* Nothing fancy and nothing Microsoft! */
>>
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>

view thread (6+ messages)  latest in thread

Message-ID: <b4651e821002220915w13f81ed4p26fb0a7126ce1fff@mail.gmail.com>
Permalink:  ../b4651e821002220915w13f81ed4p26fb0a7126ce1fff@mail.gmail.com/
Also on:    postgresql.org/message-id/b4651e821002220915w13f81ed4p26fb0a7126ce1fff@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: sycobuny@malkier.net, robjsargent@gmail.com, gwchamb@gmail.com
  Subject: Re: Join Advice and Assistance
  In-Reply-To: <b4651e821002220915w13f81ed4p26fb0a7126ce1fff@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox