postgresql:如何查询数组中有数据的jsonb列

drnojrws  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(313)

我有一张table 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"}] |
+-----------------------------------------------------------------------------------+

我想查询这些数据,并得到所有记录,其中有 Action1 用于 actions 列。它应该会回来 row 1 and 3rd 因此。

oo7oh9g9

oo7oh9g91#

您需要对数组参数使用contains运算符

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

在线示例

相关问题