X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 7BE64D1C4E2 for ; Tue, 23 Mar 2004 09:55:07 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 59939-07 for ; Tue, 23 Mar 2004 05:55:06 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id A65DDD1EC0F for ; Tue, 23 Mar 2004 05:55:03 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id i2N9t21Y056253 for ; Tue, 23 Mar 2004 09:55:02 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id i2N9sWGZ056017 for pgsql-sql@postgresql.org; Tue, 23 Mar 2004 09:54:32 GMT From: "Marcel Loose" X-Newsgroups: comp.databases.postgresql.sql Subject: Newbie Query question Date: Tue, 23 Mar 2004 10:56:12 +0100 Organization: Webplus NEWS server Lines: 28 Message-ID: X-Complaints-To: usenet@news.wplus.net X-Newsreader: Groupwise 6.5 To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200403/247 X-Sequence-Number: 17346 Hi all, I have the following problem which I will illustrate with a simplified example. I have two tables A and B. Both tables contain three columns named "objid", "owner", and "val" all of type integer. I want to select all records in A for which A.val=0 and all records in B for which both B.val=0 and B.owner=A.objid. I thought that the following query would work: SELECT * FROM A,B WHERE (A.VAL = 0) OR (B.VAL = 0 AND B.OWNER = A.OBJID); However, this query does not give me the result I expected. It appears that the database engine first calculates the cartesian product of the tables A and B and then evaluates the query. Hence, I get multiple matches for A.VAL=0 (N times the number of matching records in table A, where N is the number of records in table B). I had hoped I could somehow coerce the database engine to only use table A when evaluating the first part of the query, and use both tables A and B when evaluating the second part of the query. Is there any way to do this, other than using UNION?? Kind regards, Marcel Loose (mailto loose at astron dot nl)