【发布时间】: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[] );
错误:无法将整数类型转换为整数[]
- 任何人都可以指出定义
array expression的文档中的位置 - 澄清为什么第二次查询运行正常而第三次失败
- 阐明 ARRAY(..) 表达式的工作原理,以及类型转换为
int[]的区别是什么
【问题讨论】:
-
@a_horse_with_no_name 确实解释了一切,除了为什么第二个查询有效
标签: postgresql