访问 Postgres 数组中的值

Accessing values in Postgres array

我想要 运行 这样的查询,其中 params 是一个 text 数组:

select * from table_name where params[10]<>'Retail'  

“给我参数数组在索引 10 处不包含值 Retail 的行”

有几行应该满足该条件,即它们在该数组索引处有 'Retail' 以外的内容。但我仍然得到 0 行。

但是,如果我这样做

select * from table_name where params[10]='Retail'

然后我得到预期的行,即非 'Retail' 行被过滤掉。

此外,select params[10] from table_name 给出了一些行,其值类似于

{Retail}
{HNI}

如我所料:

Retail
HNI

这告诉我,我正在获取一个数组而不是该索引处的值。如何从数组中提取原始文本值,以便我可以在 where 子句等中使用它?

Postgres 版本:13.1

客户端:DBeaver 7.0.0

我怀疑您想要 is distinct from 而不是 <>,因此 null 值以及少于 10 个元素的数组得到了正确处理:

select * from table_name where params[10] is distinct from 'Retail'