postgresql 将文本搜索到文本数组中

postgresql search text into array of text

我有一个 table t1

id  |  names
----|-------------------------
1   |  {jully , alex , sarah}
2   |  {bety , cate , jenifer}
3   |  {adam , pit , joee}
4   |  {piter , mat , andy}

所以,我需要行至少有一个以 "a" 开头的名称 我需要的结果在下面

第 1 行:亚历克斯

第 3 行:亚当

第 4 行:安迪

id   |   names
-----|-------------------------
1    |  {jully , alex , sarah}
3    |  {adam , pit , joee}
4    |  {piter , mat , andy}

类似的查询

select * from t1 where 'a' like% any t1.name
select *
from (
    select id, unnest(names) as name
    from t
) s
where name like 'a%';
 id | name 
----+------
  1 | alex
  3 | adam
  4 | andy

聚合:

select id, array_agg(name)
from (
    select id, unnest(names) as name
    from t
) s
where name like 'a%'
group by id;
 id | array_agg 
----+-----------
  4 | {andy}
  1 | {alex}
  3 | {adam}

还有另一种使用 unnest

的解决方案
select * from t1
where exists (
  select * from unnest(t1.names) n
  where n like 'a%')

如果您必须在文本数组中搜索多个值。你可以使用这个:

SELECT * FROM t1 WHERE names && ARRAY['alex', 'jully'] ;