Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 1C0FB9FBCEC for ; Tue, 31 Jul 2007 18:09:15 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 61311-09 for ; Tue, 31 Jul 2007 18:09:03 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by postgresql.org (Postfix) with ESMTP id 3DAA09FBC50 for ; Tue, 31 Jul 2007 18:09:08 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 19DED351DA; Tue, 31 Jul 2007 14:09:07 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 1851835191; Tue, 31 Jul 2007 14:09:07 -0700 (PDT) Date: Tue, 31 Jul 2007 14:09:07 -0700 (PDT) From: Stephan Szabo To: Andreas Joseph Krogh Cc: pgsql-sql@postgresql.org Subject: Re: Alternative to INTERSECT In-Reply-To: <200707311730.51280.andreak@officenet.no> Message-ID: <20070731140503.F67596@megazone.bigpanda.com> References: <200707311730.51280.andreak@officenet.no> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/203 X-Sequence-Number: 28938 On Tue, 31 Jul 2007, Andreas Joseph Krogh wrote: > Hi all. I have the following schema: > > CREATE TABLE test ( > id integer NOT NULL, > field character varying NOT NULL, > value character varying NOT NULL > ); > > ALTER TABLE ONLY test > ADD CONSTRAINT test_id_key UNIQUE (id, field, value); > > CREATE INDEX test_like_idx ON test USING btree (id, field, value > varchar_pattern_ops); > > Using INTERSECT I want to retrieve the rows matching (pseudo-code) "firstname > LIKE ('andrea%' OR 'jose%') AND lastname LIKE 'kro%'" > > on=> SELECT t.id from test t WHERE t.field = 'firstname' AND t.value > LIKE 'andrea%' > INTERSECT SELECT t.id FROM test t WHERE t.field = 'firstname' AND t.value > LIKE 'jose%' > INTERSECT SELECT t.id FROM test t WHERE t.field = 'lastname' AND t.value > LIKE 'kro%'; Do you want something with only a firstname of jose or a firstname of jose and something other than andrea (and no others) to match or not? I'd read the pseudo-code to say yes, but AFAICT the query says no. In general, some form of self-join would probably work, but the details depend on exactly what should be returned.