Sequelize/postgres:获取与对象数组中的值匹配的记录,并提供值



>我有一个存储对象数组的数据库列。示例值为:

[{
"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
)

最新更新