【问题标题】:What is PostgreSQL ANY/SOME syntax exactly?PostgreSQL ANY/SOME 语法到底是什么?
【发布时间】:2017-05-26 02:28:35
【问题描述】:

在文档页面 https://www.postgresql.org/docs/9.6/static/functions-comparisons.html

ANY/SOME 描述如下: expression operator ANY (array expression)

给定表格:

create table ints1(
    id int
);
create table arrays1(
    ints int[]
);

和查询:

select id from ints1 where id = any (array[1, 2]);

按预期工作,因为这里解释了数组字面量语法 https://www.postgresql.org/docs/9.6/static/arrays.html

select id from ints1 where id = any (select id from ints1 where id in (1, 2));

工作正常,但为什么呢? 所以我想也许有一个从 1 列行设置为数组类型的智能类型转换,并尝试了以下查询:

insert into arrays1 (ints) values ( select id from ints1 );

不工作 - 选择附近的语法错误

然后尝试了这个:

insert into arrays1 (ints) values ( ARRAY(select id from ints1) );

这有效并将正确的值插入到arrays 表中,但我没有发现 ARRAY(..) 表达式在文档中应该如何工作。

我想也许 ARRAY(..) 又是某种类型转换运算符,并尝试使用 ::int[]:

insert into arrays1 (ints) values ( (select id from ints1)::int[] );

错误:无法将整数类型转换为整数[]

  1. 任何人都可以指出定义array expression 的文档中的位置
  2. 澄清为什么第二次查询运行正常而第三次失败
  3. 阐明 ARRAY(..) 表达式的工作原理,以及类型转换为 int[] 的区别是什么

【问题讨论】:

标签: postgresql


【解决方案1】:

【讨论】:

  • 可能是一个有价值的资源,但仅链接的答案并不是真正的事情。如果你只有这些,也许评论会更合适。
  • 你能给个其他形式的链接吗?我只能找到array expression 的表格,从而引发了整个话题。通过第二个查询,我的意思是嵌套选择 = any (select ...) 的查询
  • @MikhailBoyarsky:是贴出来的。
  • @ClodoaldoNeto 很抱歉不清楚,文档中的 other 在哪里?你发布的那个我在第一篇文章中提到过。无法通过 postgres 文档搜索和谷歌找到它。
  • @MikhailBoyarsky:更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-21
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多