在 PostgreSQL 数组中使用 LIKE 搜索
Search with LIKE in PostgreSQL array
我有这个table:
id | name | tags
----+----------+-------------------------
1 | test.jpg | {sometags,other_things}
我需要通过使用正则表达式或 LIKE
在数组中搜索来获取包含特定标签的行,如下所示:
SELECT * FROM images WHERE 'some%' LIKE any(tags);
但是这个查询returns什么都没有。
with images (id, name, tags) as (values
(1, 'test.jpg', '{sometags, other_things}'::text[]),
(2, 'test2.jpg', '{othertags, other_things}'::text[])
)
select *
from images
where (
select bool_or(tag like 'some%')
from unnest(tags) t (tag)
);
id | name | tags
----+----------+-------------------------
1 | test.jpg | {sometags,other_things}
unnest
returns a set which you aggregate with the convenient bool_or
函数
我有这个table:
id | name | tags
----+----------+-------------------------
1 | test.jpg | {sometags,other_things}
我需要通过使用正则表达式或 LIKE
在数组中搜索来获取包含特定标签的行,如下所示:
SELECT * FROM images WHERE 'some%' LIKE any(tags);
但是这个查询returns什么都没有。
with images (id, name, tags) as (values
(1, 'test.jpg', '{sometags, other_things}'::text[]),
(2, 'test2.jpg', '{othertags, other_things}'::text[])
)
select *
from images
where (
select bool_or(tag like 'some%')
from unnest(tags) t (tag)
);
id | name | tags
----+----------+-------------------------
1 | test.jpg | {sometags,other_things}
unnest
returns a set which you aggregate with the convenient bool_or
函数