Re: How to fetch rows with multiple values

From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: Sebastjan Trepca <trepca(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to fetch rows with multiple values
Date: 2006-01-20 13:30:29
Message-ID: 65B7A0D9-F7EE-4286-9B67-009CD57796AB@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Marcos 2006-01-20 13:38:32 Re: Create Produre for DROP row
Previous Message Sebastjan Trepca 2006-01-20 13:19:38 How to fetch rows with multiple values