为什么在 postgres 过程中,在任何以数组为参数的函数之前需要“=”?
Why required "=" before ANY function with array as param, in postgres procedure?
我昨天在回答一个 postgres 问题时,也遇到了一个 postgres 线程 (here),他们在其中描述了以下错误:
ERROR: operator does not exist: text = text[]
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts.
只要在不使用 = ANY
的情况下将 ARRAY 字符串类型提供给 ANY
,似乎就会出现该错误。这看起来很奇怪,因为基于语言、逻辑和 sql 约定,通常你有(例如 IN
):
variable FUNCTION(set)
而不是。
variable = FUNCTION(set)
, 除非运算符当然是 summation/count 返回一个结果的操作:)
variable ANY(Set/Array)
比 variable=ANY(Set/Array)
更有意义。类似的例子是 IN
函数。
谁能解释一下这是怎么回事?
IN (...)
基本上等同于 = ANY (ARRAY[...])
重要的是,ANY
不是函数。它是由 SQL 标准定义的语法,与 GROUP BY
或 window 函数中的 OVER
子句一样不是函数。
在ANY
之前需要=
的原因是ANY
也可以应用于其他运算符。意思是"Test the operator to the left against every element in the array on the right, and return true if the test is true for at least one element."
您可以使用 > ANY (ARRAY[...])
或其他任何方式。它是一个不限于 =
的通用运算符。对 LIKE ANY
特别有用(尽管性能有些差)。
也有 ALL
,它的作用大致相同,但 returns 只有在所有结果都为真时才为真。
我昨天在回答一个 postgres 问题时,也遇到了一个 postgres 线程 (here),他们在其中描述了以下错误:
ERROR: operator does not exist: text = text[]
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts.
只要在不使用 = ANY
的情况下将 ARRAY 字符串类型提供给 ANY
,似乎就会出现该错误。这看起来很奇怪,因为基于语言、逻辑和 sql 约定,通常你有(例如 IN
):
variable FUNCTION(set)
而不是。
variable = FUNCTION(set)
, 除非运算符当然是 summation/count 返回一个结果的操作:)
variable ANY(Set/Array)
比 variable=ANY(Set/Array)
更有意义。类似的例子是 IN
函数。
谁能解释一下这是怎么回事?
IN (...)
基本上等同于 = ANY (ARRAY[...])
重要的是,ANY
不是函数。它是由 SQL 标准定义的语法,与 GROUP BY
或 window 函数中的 OVER
子句一样不是函数。
在ANY
之前需要=
的原因是ANY
也可以应用于其他运算符。意思是"Test the operator to the left against every element in the array on the right, and return true if the test is true for at least one element."
您可以使用 > ANY (ARRAY[...])
或其他任何方式。它是一个不限于 =
的通用运算符。对 LIKE ANY
特别有用(尽管性能有些差)。
也有 ALL
,它的作用大致相同,但 returns 只有在所有结果都为真时才为真。