Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 037B14762EE for ; Fri, 18 Oct 2002 15:31:13 -0400 (EDT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id 51D184762A6 for ; Fri, 18 Oct 2002 15:31:12 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 7CA84D5F7; Fri, 18 Oct 2002 12:31:09 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 4803D5C02; Fri, 18 Oct 2002 12:31:09 -0700 (PDT) Date: Fri, 18 Oct 2002 12:31:09 -0700 (PDT) From: Stephan Szabo To: Frank Morton Cc: Subject: Re: join question In-Reply-To: <027d01c276d8$504a1100$8455e5ce@edison> Message-ID: <20021018122739.Q27388-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/246 X-Sequence-Number: 9963 On Fri, 18 Oct 2002, Frank Morton wrote: > For the SQL gurus, a query where I'm not getting the expected > results. Trying to write it using sql compatible with both postgres > and mysql. > > There are two tables: > > table = profile > int id > char name > > table = attribute > int id > int containerId > char name > char value > > Multiple attribute rows correspond to a single profile row where > attribute.containerId = profile.id > > These two queries result in one row being returned, which is > the expected result: > > select name from profile where ((profile.state='1020811')); > > select profile.name from profile,attribute where > (((attribute.name='description') and (attribute.value='1020704') and > (profile.id=attribute.containerId))); > > But, I thought this next query would just be a simple way to combine the two > queries with an "or" operator, still returning one row, actually returns > ALL rows of attribute: > > select profile.name from profile,attribute where > ((profile.state='1020811') or ((attribute.name='marketsegment') and > (attribute.value='1020704') and (profile.id=attribute.containerId))); > > Why doesn't this last query return just one row? Because for each combination of rows from profile and attribute where profile.state='1020811' the where clause is statisfied. I'm not sure what you're precisely trying to get out, since unless the row that matches each of the clauses is the same I don't see how you'd only get one row out with an or.