简体   繁体   English

如何查询在postgres中以字符串形式存在的数组?

[英]How to query array which is present as string in postgres?

I have a table called table with column called column with datatype text with values like ' ["1","2"] '.我有一个名为table的表,其列名为column ,其数据类型为text ,其值类似于' ["1","2"] '。

I need to get all records which has "1" as one of the element.我需要获取所有以“1”作为元素之一的记录。

select * 
from table 
where column.....?

How should the where clause be? where子句应该怎么写?

I think you can use ?我想你可以用? operator on jsonb type: jsonb类型的运算符:

select *
from (
    select '["1","2"]' union all
    select '["0"]'
) as a(data)
where
    a.data::jsonb ? '1'

In general, I'd consider storing your data as jsonb instead of string.一般来说,我会考虑将您的数据存储为jsonb而不是字符串。

db<>fiddle example

Simply use LIKE .只需使用LIKE Keep the double quotes to pass 1, but avoid other numbers containing that digit.保留双引号以传递 1,但避免包含该数字的其他数字。

select * 
from table 
where column like '%"1"%' 

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

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