Re: Populating an array from a select statement

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Populating an array from a select statement
Date: 2008-02-18 13:47:15
Message-ID: 20080218134715.GC23570@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

am Mon, dem 18.02.2008, um 8:22:14 -0500 mailte John Gunther folgendes:
> What's the most straightforward way to populate an array from a select
> statement? For example, using a fictional extension of SQL syntax, I'd like:
>
> update users set emails=ARRAY[select email from address where userid=25]
> where id=25;
>
> So if user 25 has emails john(at)domain(dot)com, john(at)gmail(dot)com, and
> john(at)yahoo(dot)com in the address table,
> select emails from user where id=25;
> will return:
> emails
> ----------------------------------------------------------------
> {john(at)domain(dot)com, john(at)gmail(dot)com, and john(at)yahoo(dot)com}

You can use array_to_string() and array().

Example:

test=*# select * from mail_adr ;
id | email
----+---------------
2 | foo(at)bar
2 | bar(at)batz
2 | foobar(at)barfoo
(3 rows)

test=*# select array_to_string(array(select email from mail_Adr where id=2), ', ');
array_to_string
----------------------------------
foo(at)bar, bar(at)batz, foobar(at)barfoo
(1 row)

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message John Gunther 2008-02-18 14:15:43 Re: Populating an array from a select statement
Previous Message John Gunther 2008-02-18 13:22:14 Populating an array from a select statement