gpt4 book ai didi

postgresql - 具有选择条件的 postgres 数组字段

转载 作者:行者123 更新时间:2023-11-29 12:43:04 26 4
gpt4 key购买 nike

我有一个包含 2 个字段的 POSTGRES 字段 - 整数数组字段和整数字段。

CREATE TABLE test.public.polls (
"id" serial NOT NULL,
field_1 _int4,
field_2 int4 NOT NULL,
PRIMARY KEY ("id")
);

值是

enter image description here

1) 现在我需要检查字段值 {1,2,3} 是否在 field_1 中

像这样的——

select * from test.public.polls
where field_1 = ANY ('{1,2,3}'::int[])

但这会引发错误

operator does not exist: integer[] = integer

2) 需要检查字段_1 中是否有任何 id 值 = {2,3,4}

select * from test.public.polls
where field_1 = array(id)

不确定这个的语法应该是什么。

最佳答案

由于您的 field_1 似乎是一个数组,因此以下应该有效(这称为 overlapping ):

select *
from yourtable
where field_1 && '{1,2,3}'::int[]

对于第二部分,您似乎想要聚合 id 列并检查聚合集中的任何值是否存在于 field_1 中:

select *
from yourtable
where field_1 && (select array_agg(id) from yourtable)

关于postgresql - 具有选择条件的 postgres 数组字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39430432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com