Re: list variable attributes in one select

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: list variable attributes in one select
Date: 2007-01-22 08:24:02
Message-ID: 20070122082402.GA27409@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

am Mon, dem 22.01.2007, um 8:43:48 +0100 mailte Louis-David Mitterrand folgendes:
> "person_type" contains differents caracteristics for a person (actor,
> director, author, etc.) who can have several types, hence the need for
> the person_to_type table.
>
> I'd like to know if I can list in one SELECT command a person and all of
> its types, given that the number of types can be 0 to n.
>
> For example, for a given person I'd like to obtain:
>
> "John Doe", "actor", "playright", "author"
>
> or
>
> "Jane Doe", "director"
>
> in one select.

Yes,

imagine, you have 2 tables:

test=*# select * from f1;
id | name
----+------
1 | foo
2 | bar
3 | batz
(3 rows)

test=*# select * from f2;
i_id | f1_id
------+-------
1 | 1
1 | 2
2 | 1
2 | 2
2 | 3
(5 rows)

f1 contains your characteristics, f2 contains the assignment person ->
characteristics.

Now you can see all characteristics for person with id=1:

test=*# select array_to_string(array(select name from f1 where id in (select f1_id from f2 where i_id = 1)), ', ');
array_to_string
-----------------
bar, foo
(1 row)

Hope that helps, 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

Browse pgsql-sql by date

  From Date Subject
Next Message Simon Kinsella 2007-01-22 10:50:15 FW: Possible to emulate pre-8.2 behaviour of SET CONSTRAINTS?
Previous Message Louis-David Mitterrand 2007-01-22 07:43:48 list variable attributes in one select