PostgreSQL =ANY 和 IN
PostgreSQL =ANY and IN
最近我读了 Quantified Comparison Predicates – Some of SQL’s Rarest Species:
In fact, the SQL standard defines the IN predicate as being just syntax sugar for the = ANY() quantified comparison predicate.
8.4 <in predicate>
Let RVC be the <row value predicand> and
let IPV be the <in predicate value>.
The expression RVC IN IPV
is equivalent to RVC = ANY IPV
很公平,基于其他答案,例如:What is exactly “SOME / ANY” and “IN” or Oracle: '= ANY()' vs. 'IN ()'
我假设我可以互换使用它们。
下面是我的例子:
select 'match'
where 1 = any( string_to_array('1,2,3', ',')::int[])
-- match
select 'match'
where 1 IN ( string_to_array('1,2,3', ',')::int[])
-- ERROR: operator does not exist: integer = integer[]
-- HINT: No operator matches the given name and argument type(s).
-- You might need to add explicit type casts.
问题是为什么第一个查询有效而第二个 returns 错误?
那是因为 IN
(不同于 ANY
)不接受 数组 作为输入。只有 set(来自子查询)或 list 值。详细解释:
最近我读了 Quantified Comparison Predicates – Some of SQL’s Rarest Species:
In fact, the SQL standard defines the IN predicate as being just syntax sugar for the = ANY() quantified comparison predicate.
8.4 <in predicate>
Let RVC be the <row value predicand> and
let IPV be the <in predicate value>.
The expression RVC IN IPV
is equivalent to RVC = ANY IPV
很公平,基于其他答案,例如:What is exactly “SOME / ANY” and “IN” or Oracle: '= ANY()' vs. 'IN ()' 我假设我可以互换使用它们。
下面是我的例子:
select 'match'
where 1 = any( string_to_array('1,2,3', ',')::int[])
-- match
select 'match'
where 1 IN ( string_to_array('1,2,3', ',')::int[])
-- ERROR: operator does not exist: integer = integer[]
-- HINT: No operator matches the given name and argument type(s).
-- You might need to add explicit type casts.
问题是为什么第一个查询有效而第二个 returns 错误?
那是因为 IN
(不同于 ANY
)不接受 数组 作为输入。只有 set(来自子查询)或 list 值。详细解释: