【问题标题】:Search with LIKE in PostgreSQL array在 PostgreSQL 数组中使用 LIKE 搜索
【发布时间】:2017-03-14 19:16:02
【问题描述】:

我有这张桌子:

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

我需要通过使用正则表达式或LIKE在数组中搜索来获取包含特定标签的行,如下所示:

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

但是这个查询什么也没返回。

【问题讨论】:

    标签: arrays postgresql comparison-operators


    【解决方案1】:
    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 函数聚合该集合

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 2015-08-15
      • 2013-07-25
      • 2019-08-24
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      相关资源
      最近更新 更多