加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

数组 – 具有“ANY”的PostgreSQL查询不起作用

发布时间:2020-12-13 16:39:18 所属栏目:百科 来源:网络整理
导读:SELECT "Ticket_id" FROM "Tickets" WHERE "Status" = 1 AND ("Ticket_id" != ANY(array[1,2,3])) Limit 6 结果是1,3,4,5,6 你想使用ALL,而不是任何。从 fine manual: 9.21.3. ANY/SOME (array) 06000 […] The left-hand expression is evaluated and com
SELECT "Ticket_id"  FROM "Tickets"
 WHERE "Status" = 1 AND ("Ticket_id" !=  ANY(array[1,2,3])) Limit 6

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

你想使用ALL,而不是任何。从 fine manual:

9.21.3. ANY/SOME (array)

06000

[…] 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 ANY is “true” if any true result is obtained.

所以如果我们这样说:

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

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

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

如果我们看看ALL,we see:

9.21.4. ALL (array)

06003

[…] 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 ALL is “true” if all comparisons yield true…

所以如果我们这样说:

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

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

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读