带有“ANY”的 PostgreSQL 查询不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10674032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:30:51  来源:igfitidea点击:

A PostgreSQL query with 'ANY' is not working

arrayspostgresql

提问by Bogo

SELECT "Ticket_id"  FROM "Tickets"
 WHERE "Status" = 1 AND ("Ticket_id" !=  ANY(array[1,2,3])) Limit 6

And the result is 1,2,3,4,5,6

结果是 1,2,3,4,5,6

回答by mu is too short

You want to use ALL, not ANY. From the fine manual:

你想用ALL,不是ANY。从精美的手册

9.21.3. ANY/SOME (array)

expression operator ANY (array expression)

[...] The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result. The result of ANYis "true" if any true result is obtained.

9.21.3. 任何/一些(阵列)

expression operator ANY (array expression)

[...] 使用给定的operator评估左侧表达式并将其与数组的每个元素进行比较,这必须产生布尔结果。ANY如果获得任何真结果,则结果为“真”。

So if we say this:

所以如果我们这样说:

1 != any(array[1,2])

then we'll get true since (1 != 1) or (1 != 2)is true. ANYis essentially an ORoperator. For example:

那么我们会得到真实的,因为(1 != 1) or (1 != 2)是真实的。ANY本质上是一个OR运算符。例如:

=> select id from (values (1),(2),(3)) as t(id) where id != any(array[1,2]);
 id 
----
  1
  2
  3
(3 rows)

If we look at ALL, we see:

如果我们看一下ALL,我们会看到

9.21.4. ALL (array)

expression operator ALL (array expression)

[...] The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result. The result of ALLis "true" if all comparisons yield true...

9.21.4. 所有(数组)

expression operator ALL (array expression)

[...] 使用给定的operator评估左侧表达式并将其与数组的每个元素进行比较,这必须产生布尔结果。ALL如果所有比较结果都为真,则结果为“真”...

so if we say this:

所以如果我们这样说:

1 != all(array[1,2])

then we'll get false since (1 != 1) and (1 != 2)is false and we see that ALLis essentially an ANDoperator. For example:

那么我们会得到假,因为它(1 != 1) and (1 != 2)是假的,我们看到它ALL本质上是一个AND运算符。例如:

=> select id from (values (1),(2),(3)) as t(id) where id != all(array[1,2]);
 id 
----
  3
(1 row)

If you want to exclude all values in an array, use ALL:

如果要排除数组中的所有值,请使用ALL

select "Ticket_id"
from "Tickets"
where "Status" = 1
  and "Ticket_id" != all(array[1,2,3])
limit 6

回答by Bilal Akil

Do you mean:

你的意思是:

"Ticked_id" NOT IN (1,2,3)