【问题标题】:PSQL: Find record where JSONB field with array of hashes contains some case insensitive valuePSQL:查找带有哈希数组的 JSONB 字段包含一些不区分大小写的值的记录
【发布时间】:2016-05-03 00:24:45
【问题描述】:

我需要找到 PostgreSQL 数据库 (9.5) 中的所有记录,其中 connections JSONB 列带有包含一些信息的哈希数组。并且此搜索查询必须不区分大小写。

例如列是[{"type":"email", "value":"john@test.com", "comment": "Test"}, {"type":"skype", "value":"john.b", "comment": "Test2"}])。我需要找到记录,其中connections 列包含类型为“skype”且值为“JOHN.B”的条目。

# SELECT * FROM contacts;
  id | email |  connections                              
  ---+-------+------------------------------------------------------------------------------
   1 | asd   | [{"type": "email", "value": "john@test.com"}, {"type": "skype", "value": "john.b"}]

我该怎么做?谢谢。

【问题讨论】:

    标签: postgresql where-clause jsonb


    【解决方案1】:
    WITH t(id,email,connections) AS ( VALUES
      (1,'test','[
        {"type": "email", "value": "john@test.com"},
        {"type": "skype", "value": "john.b"}
      ]'::JSONB)
    )
    SELECT * FROM t
    WHERE connections @> '[{"type": "skype"}]' 
    AND connections @> '[{"value": "john.b"}]';
    

    结果:

     id | email |                                     connections                                     
    ----+-------+-------------------------------------------------------------------------------------
      1 | test  | [{"type": "email", "value": "john@test.com"}, {"type": "skype", "value": "john.b"}]
    (1 row)
    

    【讨论】:

      【解决方案2】:

      这是使用>@的一种方法:

      where connections @> '{"type":"skype"}' and
            connections @> '{"value":"john.b"}')
      

      【讨论】:

        猜你喜欢
        • 2017-04-16
        • 1970-01-01
        • 2014-08-13
        • 1970-01-01
        • 1970-01-01
        • 2021-03-20
        • 2010-09-23
        • 2012-05-14
        • 1970-01-01
        相关资源
        最近更新 更多