【问题标题】:Search for jsonb concatenated values specified by keys搜索键指定的 jsonb 连接值
【发布时间】:2018-01-07 11:25:34
【问题描述】:

我有 jsonb 数据:

id | data
1  | {"last": "smith", "first": "john", "title": "mr"}
2  | {"last": "smith jr", "first": "john", "title": "mr"}
3  | {"last": "roberts", "first": "Julia", "title": "miss"}
4  | {"last": "2nd", "first": "john smith", "title": "miss"}

我需要搜索与“John smith”匹配的记录;所以,在这种情况下,ID - 1,2,4 我无法将每个键 => 值对的搜索分开;我需要获得连接的记录条目以检查传入的请求。

我试过了

select * from contacts where jsonb_concat(data->>'title'::TEXT || data->>'first'::TEXT || data->>'last'::TEXT) ilike "John smith";

这不起作用,因为我试图连接值而不是 jsonb 对象。有没有办法连接键指定的jsonb值?

【问题讨论】:

    标签: postgresql concatenation jsonb


    【解决方案1】:

    我自己通过一些研究解决了这个问题。 我的查询就像 -

     select * from contacts where trim(regexp_replace(CONCAT(data->>'title'::TEXT,' ',data->>'first'::TEXT,' ',data->>'last'::TEXT), '\s+', ' ', 'g')) ilike '%john%';
    

    对于 PostgreSQL 版本 > 9.1 .. 您可以使用 '\s+' 而不是 '\s+'。

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 2023-02-01
      • 2020-05-13
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      相关资源
      最近更新 更多