![](/img/trans.png)
[英]get the value that is inside an array of objects which is a value inside another object
[英]Sequelize/postgres: Get records matching a value inside an array of object with a provided value
我有一个存储 object 数组的 db 列。 一个示例值为:
[{
"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"
}
]
}]
现在,如果我想通过 sequelize 搜索具有id:1
的记录, where
子句中应该包含什么,或者是否有其他方法?
您正在寻找 json arrays 至少有一个 object 的行,其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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.