pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedJoin Advice and Assistance
6+ messages / 3 participants
[nested] [flat]
* Join Advice and Assistance
@ 2010-02-21 23:25 Gary Chambers <gwchamb@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Gary Chambers @ 2010-02-21 23:25 UTC (permalink / raw)
To: pgsql-sql
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! */
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Join Advice and Assistance
@ 2010-02-22 05:10 Rob Sargent <robjsargent@gmail.com>
parent: Gary Chambers <gwchamb@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Rob Sargent @ 2010-02-22 05:10 UTC (permalink / raw)
To: Gary Chambers <gwchamb@gmail.com>; +Cc: pgsql-sql
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
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Join Advice and Assistance
@ 2010-02-22 15:51 Gary Chambers <gwchamb@gmail.com>
parent: Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Gary Chambers @ 2010-02-22 15:51 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; +Cc: pgsql-sql
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! */
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Join Advice and Assistance
@ 2010-02-22 17:05 Rob Sargent <robjsargent@gmail.com>
parent: Gary Chambers <gwchamb@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Rob Sargent @ 2010-02-22 17:05 UTC (permalink / raw)
To: Gary Chambers <gwchamb@gmail.com>; +Cc: pgsql-sql
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! */
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Join Advice and Assistance
@ 2010-02-22 17:15 Stephen Belcher <sycobuny@malkier.net>
parent: Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Stephen Belcher @ 2010-02-22 17:15 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; +Cc: Gary Chambers <gwchamb@gmail.com>; pgsql-sql
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
>
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Join Advice and Assistance
@ 2010-02-22 22:43 Gary Chambers <gwchamb@gmail.com>
parent: Stephen Belcher <sycobuny@malkier.net>
0 siblings, 0 replies; 6+ messages in thread
From: Gary Chambers @ 2010-02-22 22:43 UTC (permalink / raw)
To: Stephen Belcher <sycobuny@malkier.net>; +Cc: Rob Sargent <robjsargent@gmail.com>; pgsql-sql
Stephen,
> 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):
Your query worked perfectly!
> 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.
Absolutely -- and thank you for the suggestion. I'll be retrieving
the results of the query through PHP, so cleanup in the query may even
be a performance degradation.
> Hope this helps,
You and Rob Sargent have helped a great deal. Thanks to both of you.
-- Gary Chambers
/* Nothing fancy and nothing Microsoft! */
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2010-02-22 22:43 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-02-21 23:25 Join Advice and Assistance Gary Chambers <gwchamb@gmail.com>
2010-02-22 05:10 ` Rob Sargent <robjsargent@gmail.com>
2010-02-22 15:51 ` Gary Chambers <gwchamb@gmail.com>
2010-02-22 17:05 ` Rob Sargent <robjsargent@gmail.com>
2010-02-22 17:15 ` Stephen Belcher <sycobuny@malkier.net>
2010-02-22 22:43 ` Gary Chambers <gwchamb@gmail.com>
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