【发布时间】:2018-01-20 13:49:10
【问题描述】:
最近看了Quantified Comparison Predicates – Some of SQL’s Rarest Species:
事实上,SQL 标准将 IN 谓词定义为 = ANY() 量化比较谓词的语法糖。
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” 或 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.
问题是为什么第一个查询有效而第二个返回错误?
【问题讨论】:
-
数组不是标准 SQL 的一部分。 Postgres 将第二个解释为说“1”等于整个数组,而不是它的特定组件。
-
@GordonLinoff 是的,我知道该数组是 PostgreSQL 扩展。您能否通过文档链接提供更详细的答案?也许与运算符重载在当前情况下的工作方式有关。
-
in (scalar list)和= any (array)有效。in(array)没有记录可以工作。对吧?.. -
文档指出
in(subquery)等于= any(subquery)postgresql.org/docs/current/static/… 但= any(array)不等于in(array),即使事实上如果in(scalar list)有不止一项,规划师将其重写为=any(array),这并不意味着in(array)可以工作...
标签: sql postgresql sql-in