Re: Alternative to INTERSECT
| От | Stephan Szabo |
|---|---|
| Тема | Re: Alternative to INTERSECT |
| Дата | |
| Msg-id | 20070731140503.F67596@megazone.bigpanda.com обсуждение исходный текст |
| Ответ на | Alternative to INTERSECT (Andreas Joseph Krogh <andreak@officenet.no>) |
| Список | 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 по дате отправления: