Thread: list variable attributes in one select

list variable attributes in one select

From
Louis-David Mitterrand
Date:
Hello,

I've got the following tables:

person:
- id_person
- firstname
- lastname
- type

person_to_type:
- id_person references person
- type references person_type;

person_type:
- type

"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.

Is that possible?



Re: list variable attributes in one select

From
"A. Kretschmer"
Date:
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