【问题标题】:find all rows that for a postgres jsonb column that have a specfic array value查找具有特定数组值的 postgres jsonb 列的所有行
【发布时间】:2021-08-06 23:57:26
【问题描述】:

(使用 Postgres 9.5)

假设您有一个具有以下架构的 Postgres 表:


                                            Table "public.tableA"
        Column     |           Type           | Collation | Nullable |                   Default
    ---------------+--------------------------+-----------+----------+----------------------------------------------
     id            | bigint                   |           | not null | nextval('truelayer_tokens_id_seq'::regclass)
     user_id       | text                     |           | not null |
     created_at    | timestamp with time zone |           |          | now()
     updated_at    | timestamp with time zone |           |          |
     provider_id   | text                     |           | not null | ''::text
     scopes        | jsonb                    |           | not null | '{}'::jsonb

在范围列内是各种 user_id 的以下数据的子集。 ["A", "B", "C", "D", "E"]

  • 有些用户会有范围:["A"]
  • 有些用户会有范围:["B","C","D"]
  • 有些用户会有范围:["A","D"]

我想运行一个查询,返回特定 user_id 的所有行,其中“范围”中的数组包含值“A”

我什至很难开始使用这个,所以所有建议都值得赞赏。

【问题讨论】:

    标签: postgresql jsonb postgresql-9.5


    【解决方案1】:

    使用jsonb 遏制运算符@>

    SELECT id, scopes
    FROM "tableA"
    WHERE scopes @> '["A"]';
    

    【讨论】:

      猜你喜欢
      • 2022-07-02
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 2014-12-11
      相关资源
      最近更新 更多