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

From Michael Glaesemann
Subject Re: How to fetch rows with multiple values
Date
Msg-id 65B7A0D9-F7EE-4286-9B67-009CD57796AB@myrealbox.com
Whole thread Raw
In response to How to fetch rows with multiple values  (Sebastjan Trepca <trepca@gmail.com>)
Responses Re: How to fetch rows with multiple values
List pgsql-general
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: Sebastjan Trepca
Date:
Subject: How to fetch rows with multiple values
Next
From: Sebastjan Trepca
Date:
Subject: Re: How to fetch rows with multiple values