Re: Alternative to INTERSECT - Mailing list pgsql-sql

From Stephan Szabo
Subject Re: Alternative to INTERSECT
Date
Msg-id 20070731140503.F67596@megazone.bigpanda.com
Whole thread Raw
In response to Alternative to INTERSECT  (Andreas Joseph Krogh <andreak@officenet.no>)
List pgsql-sql
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.


pgsql-sql by date:

Previous
From: "Rodrigo De León"
Date:
Subject: Re: Alternative to INTERSECT
Next
From: "John Mitchell"
Date:
Subject: How do I connect postgres table structures and view structures to an existing svn repository?