X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id AD9999DCAE6 for ; Sun, 4 Dec 2005 09:31:22 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 47393-06 for ; Sun, 4 Dec 2005 09:31:17 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from 137086.vserver.de (vs137086.vserver.de [62.75.137.86]) by postgresql.org (Postfix) with ESMTP id 8B7BC9DCAA9 for ; Sun, 4 Dec 2005 09:31:18 -0400 (AST) Received: from a-kretschmer.de (p54B3BDB7.dip0.t-ipconnect.de [84.179.189.183]) (authenticated bits=0) by 137086.vserver.de (8.12.8/8.12.8) with ESMTP id jB4DVIPn028706 for ; Sun, 4 Dec 2005 14:31:20 +0100 Received: from kretschmer by a-kretschmer.de with local (Exim 3.36 #1) id 1Eiu2D-0000jn-00 for pgsql-sql@postgresql.org; Sun, 04 Dec 2005 14:36:09 +0100 Date: Sun, 4 Dec 2005 14:36:09 +0100 From: Andreas Kretschmer To: pgsql-sql@postgresql.org Subject: Re: SQL Statement Help needed Message-ID: <20051204133609.GA2792@kaufbach.delug.de> References: Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-GPG-Fingerprint: EE16 3C01 7B9C 10F7 2C8B 3B86 4DB3 D9EE 7F45 84DA X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! X-Lugdd: Gerd Kube X-Info: My name is root. Just root. And I am licensed to kill -9 User-Agent: mutt-ng 1.5.9i (Linux) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200512/15 X-Sequence-Number: 23557 Michael Avila 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°