Re: How to fetch rows with multiple values - Mailing list pgsql-general

From Sebastjan Trepca
Subject Re: How to fetch rows with multiple values
Date
Msg-id cd329af80601200547v2f835e0dl4fcc4c9503cc18d4@mail.gmail.com
Whole thread Raw
In response to Re: How to fetch rows with multiple values  (Michael Glaesemann <grzm@myrealbox.com>)
List pgsql-general
Wow, this joined query is super faster then intersect(10x), thanks a lot!!

Regarding that I have to make a join for every term, I would think it would be more consuming. Is there any limit of joins or something similar which I should be aware of?

Sebastjan


On 1/20/06, Michael Glaesemann <grzm@myrealbox.com> wrote:

On Jan 20, 2006, at 22:19 , Sebastjan Trepca wrote:

> What I would like is to write a query where I can specify multiple
> names and get the IDs which have them.
>
> For now it seems the most efficient way is to use INTERSECT statement:
>
> SELECT "ID" from customer_mapping WHERE "Name"='john'
> INTERSECT
> SELECT "ID" from customer_mapping WHERE "Name"='peter'

My first thought is to use a join. Does this do what you want?

select id
from customer_mapping cm1
join customer_mapping cm2 using ("ID")
where cm1."Name" = 'john
and cm2."Name" = 'peter';

> Although, I don't know how exactly to use ORDER, OFFSET and LIMIT
> in this case...

ORDER, OFFSET and LIMIT should work just fine with the JOIN query.
You could also use your intersect in a subquery and then use ORDER,
OFFSET and LIMIT on the outer query, e.g.,

select *
from (
        select "ID"...
        intersect
        select "ID" ...
) as common_names
...

Michael Glaesemann
grzm myrealbox com




pgsql-general by date:

Previous
From: Michael Glaesemann
Date:
Subject: Re: How to fetch rows with multiple values
Next
From: Martijn van Oosterhout
Date:
Subject: Re: How to convert Big5 to UTF8