简体   繁体   English

访问 Postgres 数组中的值

[英]Accessing values in Postgres array

I want to run a query like this where params is a text array:我想运行这样的查询,其中params是一个text数组:

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

"Give me rows whose params array does not contain the value Retail at index 10" “给我参数数组不包含索引 10 处的值零售的行”

There are several rows that should statisfy that condition, ie they have something other than 'Retail' at that array index.有几行应该满足该条件,即它们在该数组索引处具有“零售”以外的其他内容。 But still I get 0 rows.但我仍然得到 0 行。

However, if I do但是,如果我这样做

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

Then I get rows as expected ie non 'Retail' rows are filtered out.然后我按预期得到行,即过滤掉非“零售”行。

Also, select params[10] from table_name gives a number of rows with values like此外, select params[10] from table_name给出了许多行,其值如下

{Retail}
{HNI}

Where as I am expecting:正如我所期待的:

Retail
HNI

This tells me, I am getting an array instead of a the value at that index.这告诉我,我得到的是一个数组而不是该索引处的值。 How do I extract the raw text value from an array so that I can use it in where clause etc.?如何从数组中提取原始文本值,以便可以在 where 子句等中使用它?

Postgres version: 13.1 Postgres 版本:13.1

Client: DBeaver 7.0.0客户端:DBeaver 7.0.0

I suspect you want is distinct from instead if <> , so null values are properly handled, as well as arrays that have less than 10 elements:我怀疑你想要is distinct from if <> ,所以null值得到正确处理,以及 arrays 的元素少于 10 个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM