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 1E6449DC98B for ; Mon, 20 Feb 2006 07:04:00 -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 63696-08 for ; Mon, 20 Feb 2006 07:04:02 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from smtp3.lc.anpe.fr (unknown [80.124.143.18]) by postgresql.org (Postfix) with ESMTP id BE1439DC86A for ; Mon, 20 Feb 2006 07:03:57 -0400 (AST) Received: from HOMERE02.anpe.fr (localhost.localdomain [127.0.0.1]) by smtp3.lc.anpe.fr (antivirus) with ESMTP id B72ED73C80 for ; Mon, 20 Feb 2006 12:03:55 +0100 (CET) Received: from [10.0.32.170] ([10.0.32.170]) by HOMERE02.anpe.fr with Microsoft SMTPSVC(6.0.3790.1830); Mon, 20 Feb 2006 12:01:58 +0100 Message-ID: <43F9A1BA.4020305@anpe.fr> Date: Mon, 20 Feb 2006 12:02:18 +0100 From: Patrick JACQUOT User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: SELECT on a to-be-determined table References: <1139930910.624290.83370@g44g2000cwa.googlegroups.com> In-Reply-To: <1139930910.624290.83370@g44g2000cwa.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 20 Feb 2006 11:01:58.0025 (UTC) FILETIME=[115A6790:01C6360D] X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.129 required=5 tests=[AWL=0.129] X-Spam-Score: 0.129 X-Spam-Level: X-Archive-Number: 200602/247 X-Sequence-Number: 24257 garhone wrote: >Hi, > >I'm a new at this. So please forgive if I mess up. Also, if there is >already a reference/tutorial somewhere, feel free to point me to it. > >Here's my situation: >db=# select * from projects; > projid | projname >--------+---------- > 1 | cars > 2 | houses > 3 | pets >(3 rows) > >db=# select * from cars; > carid | carname >-------+--------- > 1 | ford > 2 | mazda >(2 rows) > >db=# select * from houses; > houseid | housename >---------+----------- > 1 | semi > 2 | trailer > 3 | mansion >(3 rows) > >db=# select * from pets; > petid | petname >-------+--------- > 1 | dog > 2 | cat > 3 | bird >(3 rows) > >Is it possible to do this: >Give me all the rows of the table whose project id is 2 (or whatever >projid). > >Thanks > > >---------------------------(end of broadcast)--------------------------- >TIP 4: Have you searched our list archives? > > http://archives.postgresql.org > > Your way of thinking leads to the need of comparing a field to a table name. Such a request requires two steps 1 - retrieve the name of the table to search in, store it in a variable 2 - use execute to issue a request to that table. Instead, I think it would be better to use only two tables: 1 - projects (projid, projname) 2 - items (itemid, projid, itemproperty1,itemidproperty2,...) You would have in the second table, to take your example: projid | itemid | itemname | 1 | 1 | ford | 1 | 2 | mazda | 2 | 1 | semi | 2 | 2 | trailer | 2 | 3 | mansion | 3 | 1 | dog | 3 | 2 | cat | 3 | 3 | bird | Your request would become : SELECT itemid, itemname FROM items where projid=2 The problem of having a different set of properties for the items of differents projects could be solved with three tables: project(projid, projname) itempropertymeaning(projid, propid, propmeaning) itemproperty(projid, itemid, propid, propvalue)