【问题标题】:Postgres SQL: How to query jsonb column having data in arrayPostgresql:如何查询数组中有数据的jsonb列
【发布时间】:2020-10-19 05:45:48
【问题描述】:

我有一个表customrule 有一个结构

id.       - int
name      - varchar
actions.  - jsonb

我已经阅读了-> 运算符。但它似乎不适用于我的情况,因为我将数据存储为数组。

+-----------------------------------------------------------------------------------+
| Name | Id | Actions                                                               |
+-----------------------------------------------------------------------------------+
| CR-1 | 1  | [{"name": "Action1", "count": "1"},{"name": "Action2", "count": "2"}] |
+-------------------+---------------------------------------------------------------+
| CR-2 | 2  | [{"name": "Action5", "count": "1"},{"name": "Action4", "count": "2"}] |
+-----------------------------------------------------------------------------------+
| CR-3 | 3  | [{"name": "Action1", "count": "1"},{"name": "Action1", "count": "2"}] |
+-----------------------------------------------------------------------------------+

我想查询这些数据并获取在actions 列中使用了Action1 的所有记录。结果应该返回row 1 and 3rd

【问题讨论】:

    标签: sql postgresql jsonb


    【解决方案1】:

    您需要将包含运算符与数组参数一起使用

    select id, name, actions
    from customrule 
    where actions @> '[{"name": "Action1"}]'
    

    Online example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多