在任何数组的项目中查找文本

Find text in any array's items

状态列的类型为 jsonb

示例:

[{
        "dt": "2020-12-18T16:19:45.378+00:00",
        "id": "confirmed"
    },
    {
        "dt": "2020-12-16T12:11:45.210+00:00",
        "id": "delivery"
    },
    {
        "dt": "2020-12-16T11:25:08.444+00:00",
        "id": "new"

    }
]

如果我想查找数组中的第一项 id = confirmed

我用这个

select id, states from my_table where states->0->>'id' = 'confirmed'

不错。工作正常。

但是...我需要检查任何数组项中是否存在 id = confirmed。 可能吗?

您可以使用包含运算符 @>

select id, states 
from my_table 
where states @> '[{"id": "confirmed"}]'