>我有一个存储对象数组的数据库列。示例值为:
[{
"recordId": 11,
"column": [{
"name": "some value",
"id": "1"
},
{
"name": "other value",
"id": "2"
}
]
}, {
"recordId": 12,
"column": [{
"name": "some value",
"id": "3"
},
{
"name": "other value",
"id": "4"
}
]
}]
现在,如果我想搜索通过后遗症id:1
记录,where
子句中应该有什么,或者有其他方法吗?
您正在寻找其 json 数组至少有一个对象的行,该对象的id
属性具有值1
。在纯 SQL 中,您可以将exists
与取消嵌套数组的相关子查询一起使用:
select t.*
from mytable t
where exists (
select 1
from jsonb_array_elements(t.column) as x(obj)
where (x.obj ->> 'id')::int = 1
)