Re: SQL Statement Help needed - Mailing list pgsql-sql

From Andreas Kretschmer
Subject Re: SQL Statement Help needed
Date
Msg-id 20051204133609.GA2792@kaufbach.delug.de
Whole thread Raw
In response to SQL Statement Help needed  ("Michael Avila" <Michael.Avila.1@sbcglobal.net>)
List pgsql-sql
Michael Avila <Michael.Avila.1@sbcglobal.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°


pgsql-sql by date:

Previous
From: "Michael Avila"
Date:
Subject: SQL Statement Help needed
Next
From: Theodore Petrosky
Date:
Subject: Re: question using 'between' in a sql query