user1389960
user1389960

Reputation: 433

Multiple fields in an IN clause?

Is it possible to include multiple fields in an IN clause? Something like the following?

select * from data
where id1, id2 in (select id from dataset)

I would like the query to return those cases in data in which the id1 and/or id2 fields appear in the id field in dataset.

I am using postgresql.

Upvotes: 0

Views: 81

Answers (3)

Abdul Rasheed
Abdul Rasheed

Reputation: 6719

try this,

select  * 
from    data
where   exists (select  id 
                from    dataset 
                where   id = id1
                    OR  id = id2
                    ...
                    OR  id = id5)

Upvotes: 2

Mischa
Mischa

Reputation: 2298

If you have five ids and you know they are distinct, you can do. select ... from data where 5=(select count(*) from dataset where id in (id1, id2, ... id5))

Upvotes: 0

John Woo
John Woo

Reputation: 263723

how about converting it into JOIN since such function you are looking does not exist?

select a.* 
from data a INNER JOIN dataset b
    ON a.id1 = b.id OR a.id2 = b.id 

Upvotes: 3

Related Questions