pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: A. Kretschmer <andreas.kretschmer@schollglas.com>
To: pgsql-sql@postgresql.org
Subject: Re: list variable attributes in one select
Date: Mon, 22 Jan 2007 09:24:02 +0100
Message-ID: <20070122082402.GA27409@a-kretschmer.de> (raw)
In-Reply-To: <20070122074348.GA8177@apartia.fr>
References: <20070122074348.GA8177@apartia.fr>

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



view thread (2+ messages)

Message-ID: <20070122082402.GA27409@a-kretschmer.de>
Permalink:  ../20070122082402.GA27409@a-kretschmer.de/
Also on:    postgresql.org/message-id/20070122082402.GA27409@a-kretschmer.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: andreas.kretschmer@schollglas.com
  Subject: Re: list variable attributes in one select
  In-Reply-To: <20070122082402.GA27409@a-kretschmer.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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