【问题标题】:PostgreSQL =ANY and IN [duplicate]PostgreSQL =ANY 和 IN [重复]
【发布时间】: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.

DB Fiddle

问题是为什么第一个查询有效而第二个返回错误?

【问题讨论】:

  • 数组不是标准 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


【解决方案1】:

这是因为IN(与ANY 不同)不接受数组 作为输入。只有一个 set(来自子查询)或一个 list 值。详细解释:

【讨论】:

  • 好的,感谢您在链接问题中的回答,我已将我的问题标记为重复
  • @lad2025:是的,看起来很合适。
猜你喜欢
  • 2016-04-10
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多