public inbox for [email protected]
help / color / mirror / Atom feedTurning a subselect into an array
5+ messages / 2 participants
[nested] [flat]
* Turning a subselect into an array
@ 2004-10-28 22:37 Jim C. Nasby <[email protected]>
0 siblings, 1 reply; 5+ messages in thread
From: Jim C. Nasby @ 2004-10-28 22:37 UTC (permalink / raw)
To: [email protected]
I'm sure this has been answered before, but the search seems to be down
again.
How can I convert the results of a subselect into an array? IE:
CREATE TABLE a(a int, b int, c int[]);
INSERT INTO table_a
SELECT a, b, (SELECT c FROM table_c WHERE table_c.parent = table_b.id)
FROM table_b
;
--
Jim C. Nasby, Database Consultant [email protected]
Give your computer some brain candy! www.distributed.net Team #1828
Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Turning a subselect into an array
@ 2004-10-28 23:21 Michael Fuhr <[email protected]>
parent: Jim C. Nasby <[email protected]>
0 siblings, 1 reply; 5+ messages in thread
From: Michael Fuhr @ 2004-10-28 23:21 UTC (permalink / raw)
To: Jim C. Nasby <[email protected]>; +Cc: [email protected]
On Thu, Oct 28, 2004 at 05:37:29PM -0500, Jim C. Nasby wrote:
> I'm sure this has been answered before, but the search seems to be down
> again.
>
> How can I convert the results of a subselect into an array? IE:
>
> CREATE TABLE a(a int, b int, c int[]);
> INSERT INTO table_a
> SELECT a, b, (SELECT c FROM table_c WHERE table_c.parent = table_b.id)
> FROM table_b
See the "Array Constructors" section in the PostgreSQL documentation:
http://www.postgresql.org/docs/7.4/static/sql-expressions.html#SQL-SYNTAX-ARRAY-CONSTRUCTORS
INSERT INTO table_a
SELECT a, b, ARRAY(SELECT c FROM table_c WHERE table_c.parent = table_b.id)
FROM table_b
--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Turning a subselect into an array
@ 2004-10-29 22:13 Jim C. Nasby <[email protected]>
parent: Michael Fuhr <[email protected]>
0 siblings, 1 reply; 5+ messages in thread
From: Jim C. Nasby @ 2004-10-29 22:13 UTC (permalink / raw)
To: Michael Fuhr <[email protected]>; +Cc: [email protected]
Ok, next stupid question that I can't find in the docs... How would I
join a table to an array? IE: if I have an array of primary keys for
some table and I want to get the name field from that table and turn it
back into an array, how would I do that?
On Thu, Oct 28, 2004 at 05:21:52PM -0600, Michael Fuhr wrote:
> On Thu, Oct 28, 2004 at 05:37:29PM -0500, Jim C. Nasby wrote:
> > I'm sure this has been answered before, but the search seems to be down
> > again.
> >
> > How can I convert the results of a subselect into an array? IE:
> >
> > CREATE TABLE a(a int, b int, c int[]);
> > INSERT INTO table_a
> > SELECT a, b, (SELECT c FROM table_c WHERE table_c.parent = table_b.id)
> > FROM table_b
>
> See the "Array Constructors" section in the PostgreSQL documentation:
>
> http://www.postgresql.org/docs/7.4/static/sql-expressions.html#SQL-SYNTAX-ARRAY-CONSTRUCTORS
>
> INSERT INTO table_a
> SELECT a, b, ARRAY(SELECT c FROM table_c WHERE table_c.parent = table_b.id)
> FROM table_b
>
> --
> Michael Fuhr
> http://www.fuhr.org/~mfuhr/
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings
>
--
Jim C. Nasby, Database Consultant [email protected]
Give your computer some brain candy! www.distributed.net Team #1828
Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Turning a subselect into an array
@ 2004-10-30 04:40 Michael Fuhr <[email protected]>
parent: Jim C. Nasby <[email protected]>
0 siblings, 1 reply; 5+ messages in thread
From: Michael Fuhr @ 2004-10-30 04:40 UTC (permalink / raw)
To: Jim C. Nasby <[email protected]>; +Cc: [email protected]
On Fri, Oct 29, 2004 at 05:13:02PM -0500, Jim C. Nasby wrote:
> Ok, next stupid question that I can't find in the docs... How would I
> join a table to an array? IE: if I have an array of primary keys for
> some table and I want to get the name field from that table and turn it
> back into an array, how would I do that?
See the "Subquery Expressions" and "Row and Array Comparisons"
sections in the PostgreSQL documentation:
http://www.postgresql.org/docs/7.4/static/functions-subquery.html
http://www.postgresql.org/docs/7.4/static/functions-comparisons.html
If I understand you correctly, this should work:
SELECT ARRAY(SELECT name FROM foo WHERE id = ANY(ARRAY[1,2,3]));
A test I just ran showed "id = ANY(ARRAY[1,2,3])" doing a sequential
scan whereas "id IN (1,2,3)" did an index scan, so you might want to
use the latter if possible.
--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: [GENERAL] Turning a subselect into an array
@ 2004-10-30 06:07 Jim C. Nasby <[email protected]>
parent: Michael Fuhr <[email protected]>
0 siblings, 0 replies; 5+ messages in thread
From: Jim C. Nasby @ 2004-10-30 06:07 UTC (permalink / raw)
To: Michael Fuhr <[email protected]>; +Cc: [email protected]; pgsql-docs
Thanks again for the help. I did manage to find that after a bunch of
searching in the mailing list. There should really be a portion of the
docs dedicated to array handling.
On Fri, Oct 29, 2004 at 10:40:57PM -0600, Michael Fuhr wrote:
> On Fri, Oct 29, 2004 at 05:13:02PM -0500, Jim C. Nasby wrote:
> > Ok, next stupid question that I can't find in the docs... How would I
> > join a table to an array? IE: if I have an array of primary keys for
> > some table and I want to get the name field from that table and turn it
> > back into an array, how would I do that?
>
> See the "Subquery Expressions" and "Row and Array Comparisons"
> sections in the PostgreSQL documentation:
>
> http://www.postgresql.org/docs/7.4/static/functions-subquery.html
> http://www.postgresql.org/docs/7.4/static/functions-comparisons.html
>
> If I understand you correctly, this should work:
>
> SELECT ARRAY(SELECT name FROM foo WHERE id = ANY(ARRAY[1,2,3]));
>
> A test I just ran showed "id = ANY(ARRAY[1,2,3])" doing a sequential
> scan whereas "id IN (1,2,3)" did an index scan, so you might want to
> use the latter if possible.
>
> --
> Michael Fuhr
> http://www.fuhr.org/~mfuhr/
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to [email protected])
>
--
Jim C. Nasby, Database Consultant [email protected]
Give your computer some brain candy! www.distributed.net Team #1828
Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2004-10-30 06:07 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-10-28 22:37 Turning a subselect into an array Jim C. Nasby <[email protected]>
2004-10-28 23:21 ` Michael Fuhr <[email protected]>
2004-10-29 22:13 ` Jim C. Nasby <[email protected]>
2004-10-30 04:40 ` Michael Fuhr <[email protected]>
2004-10-30 06:07 ` Jim C. Nasby <[email protected]>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox