如何在Postgres中用JOIN替换IN子句



我有以下查询。

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))

如何将IN子句替换为JOIN,如以下Postgres中所示。

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

编辑:我从某处读到IN运算符不使用索引。此外,如果传递更多参数,则此查询将花费更多时间。

我不知道你为什么要这么做,因为实际上它们之间没有区别。您可以使用下面的查询并使用CTE创建临时表并连接在一起。

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

最新更新