我有一个名为cust_data的表,它存储了id和JSON对象。我想写Postgres的select语句获取:Postgres查询JSON数组
- 选择所有的id其中“性别”:“女”是不存在的人数组[这应该从下面的数据返回ID#3]
- 选择所有的id其中, “性别”: “女性” 是本和 “状态”: “已婚”
表[这应该从下面的数据返回ID#2]:cust_data
id(numeric) | connections (jsonb)
------------------------------
1, {"Persons": [
{
"personName": "Tom",
"gender": "Male",
"country": "USA",
"status":"single"
},
{
"personName": "Harry",
"gender": "Male",
"country": "USA",
"status":"single"
},
{
"personName": "Lisa",
"gender": "Female",
"country": "Mexico",
"status":"single"
}
]
}
2,{
"Persons": [
{
"personName": "Lisa",
"gender": "Male",
"country": "UK",
"status":"single"
},
{
"personName": "Harry",
"gender": "Male",
"country": "USA",
"status":"single"
},
{
"personName": "Lisa",
"gender": "Female",
"country": "Mexico",
"status":"married"
}
]
}
3,{
"Persons": [
{
"personName": "Lisa",
"gender": "Male",
"country": "UK",
"status":"single"
},
{
"personName": "Harry",
"gender": "Male",
"country": "USA",
"status":"single"
}
]
}
你看官方的Postgres页:https://www.postgresql.org/docs/9.3/static/functions-json .html –
是的。但它拉出所有记录。这里是查询 - 从cust_data t,jsonb_array_elements(t.connections - >'persons')的SELECT id AS elem WHERE elem - >>'gender'!='female'; – user842122
请检查人员。在你的例子中它是大写字母。而在查询时,它是用小写字母写的。 –