简体   繁体   中英

How to replace IN clause with JOIN in Postgres?

I have the following query.

select * 
from table table0_ 
where (table0_.col1, table0_.col2, table0_.col3) in (($1, $2, $3) , ($4, $5, $6) , ($7, $8, $9) , ($10, $11, $12) , ($13, $14, $15))

How to replace IN clause with JOIN as shown in the below in the Postgres.

select * 
from table0_ 
where table0_.col1=$1 
  and table0_.col2=$2 
  and table0_.col3=$3

EDIT: I read from somewhere that IN operator does not make use of indexes. Also, this query is taking more time if passing more parameters.

I don't know why you should do that because actually no difference between them. You can use the below query and use CTE to create a temp table and join together.

with data as (
  select 
    *
  from (
    values ($1, $2, $3) , ($4, $5, $6) , ($7, $8, $9) , ($10, $11, $12) , ($13, $14, $15)
  ) t (col1, col2, col3)
)
select 
  table0_.*
from 
  table0_, data
where 
  table0_.col1 = data.col1
  and table0_.col2 = data.col2
  and table0_.col3 = $data.col3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM