2016-11-01 55 views
1

我有這個表:與LIKE搜索在PostgreSQL的陣列

id | name |   tags   
----+----------+------------------------- 
    1 | test.jpg | {sometags,other_things} 

我需要在陣列與正則表達式或LIKE搜索,這樣獲得包含特定標籤行:

SELECT * FROM images WHERE 'some%' LIKE any(tags); 

但是這個查詢什麼都沒有返

回答

4
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返回你與便利bool_or功能聚集一組