在PostgreSQL中按数组从json / jsonb列中查找数据

时间:2019-11-08 16:55:58

标签: sql postgresql

我有一列包含json / jsonb数据。数据库是postgresql。

列数据示例

{"postId": "p522", "groupId": "g11", ...}
{"postId": "p5ds", "groupId": "g234", ...}
{"postId": "p5cz", "groupId": "g597", ...}

我正在尝试从json / jsonb列中搜索为数据数组。

什么是最好的或正确的方法?

SELECT * FROM column_name
WHERE 
-- How i can do, similar to this? 
data #> '{groupId}' IN ('g11', 'g597')

-- this works but only for single. I am trying to find by array. 
meta @> '{"groupId": "g597"}' 

2 个答案:

答案 0 :(得分:2)

如果您想要类似于?|的内容,则可以使用运算符IN。它适用于jsonb类型。

SELECT * FROM column_name
WHERE meta -> 'groupId' ?| array['g11', 'g597']

More info about json/jsonb operators

答案 1 :(得分:0)

已解决:

SELECT * FROM column_name 
WHERE meta ->> 'groupId' IN ('g11', 'g597')