pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Joseph Krogh <andreak@officenet.no>
To: pgsql-sql@postgresql.org
Subject: Alternative to INTERSECT
Date: Tue, 31 Jul 2007 17:30:51 +0000
Message-ID: <200707311730.51280.andreak@officenet.no> (raw)

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%';
 id
----
  1
(1 row)

Is there a way to make this more efficient with another construct, or 
INTERSECT the only way to accomplish the desired result?

--
Andreas Joseph Krogh



view thread (6+ messages)  latest in thread

Message-ID: <200707311730.51280.andreak@officenet.no>
Permalink:  ../200707311730.51280.andreak@officenet.no/
Also on:    postgresql.org/message-id/200707311730.51280.andreak@officenet.no

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: andreak@officenet.no
  Subject: Re: Alternative to INTERSECT
  In-Reply-To: <200707311730.51280.andreak@officenet.no>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox