Re: SQL Statement Help needed

From: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: SQL Statement Help needed
Date: 2005-12-04 13:36:09
Message-ID: 20051204133609.GA2792@kaufbach.delug.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Michael Avila <Michael(dot)Avila(dot)1(at)sbcglobal(dot)net> schrieb:

> I am not much of a SQL guru so I am having trouble trying to figure out how
> to format a SQL statement.
>
> I have a table with members named members. Each member has only 1 record.
> Then I have a table with member telephone numbers in it name
> membertelephones. A member can have more than one telephone number (home,
> work, cell, pager, fax, etc.). I want to print out the telephone numbers of

test=# select * from member;
id | name
----+---------
1 | andreas
2 | anja
(2 rows)

test=# select * from member_number ;
id | number
----+--------
1 | 12345
1 | 45678
2 | 232323
(3 rows)

> the members. Is it possible to do it in one SQL statement like with a JOIN
> or something or do I need to get the members and then loop through the
> membertelephones to get the telephone numbers? Is it possible to do a JOIN
> with a table with one record with a table with multiple records?
>
> SELECT * FROM member
>
> SELECT * FROM membertelephone WHERE member_id = the id from the above SELECT

test=# select * from member_number where id = (select id from member
where name = 'andreas');
id | number
----+--------
1 | 12345
1 | 45678
(2 rows)

Btw.: you should use referential integrity between this tables:

t=# \d member_number
Table "public.member_number"
Column | Type | Modifiers
--------+-------------------+-----------
id | integer |
number | character varying |
Foreign-key constraints:
"member_number_id_fkey" FOREIGN KEY (id) REFERENCES member(id)

Btw.: on your mail there was a odd attachment 'winmail.dat'

HTH, Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Theodore Petrosky 2005-12-04 16:28:54 Re: question using 'between' in a sql query
Previous Message Michael Avila 2005-12-04 12:31:58 SQL Statement Help needed