【问题标题】:How to SELECT * WHERE JSON keys contain ANY/IN (array of values)如何选择 * WHERE JSON 键包含 ANY/IN(值数组)
【发布时间】:2019-06-19 00:31:44
【问题描述】:

我正在处理 Symfony“工作流程”“标记”和 Postgres。 (和教义)

在数据库中,“状态”列包含这样的 JSON 数据

{"needs_address":1,"needs_contacts":1,"needs_education":1,"needs_health_and_social":1,"has_profile_photo":1,"has_letter":1}

我想出了如何像这样查询

SELECT id, profile_status, beneficiary_code
FROM public.beneficiary_profile
WHERE profile_status->>'needs_address' = '1'

如何查询状态列表'like
('needs_education','needs_contacts','needs_address')
不用像

那样全部写出来
WHERE profile_status->>'needs_address' = '1'
    OR profile_status->>'needs_contacts' = '1'
    OR profile_status->>'needs_education' = '1'

我认为必须有一种 JSON 函数的方法,可能是 IN()ANY()

【问题讨论】:

    标签: json postgresql symfony doctrine-query


    【解决方案1】:

    您需要取消嵌套元素才能使用 IN 子句,例如:

    select *
    from public.beneficiary_profile p
    where exists (select *
                  from jsonb_each_text(p.status) as x(ky,val)
                  where x.ky in ('needs_address', 'needs_contact', 'needs_education')
                    and x.val = '1');
    

    【讨论】:

      猜你喜欢
      • 2020-08-15
      • 1970-01-01
      • 2018-02-13
      • 2021-03-12
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      相关资源
      最近更新 更多