相关疑难解决方法(0)

Postgres JSONB:数组数组的where子句

在postgres(第9.5节,如果有关系):

create table json_test(
 id varchar NOT NULL,
 data jsonb NOT NULL,
 PRIMARY KEY(id)
);
Run Code Online (Sandbox Code Playgroud)

数据是json并包含数组的数组

{
    "attribute": "0",
    "array1": [{
        "id": "a12",
        "attribute": "1",
        "array2": [{
            "id": "a21",
            "attribute": "21"
        }]
    },
    {
        "id": "a12",
        "attribute": "2",
        "array2": [{
            "id": "22",
            "attribute": "22"
        }]
    }]
}
Run Code Online (Sandbox Code Playgroud)

需要:

select id from json_test where 
    json_test->>'attribute'='0' and
    array1.[id='a12'].array2.attribute='22'
Run Code Online (Sandbox Code Playgroud)

查询应该意味着:给我所有的ID

  1. 一些顶级属性具有特定值
  2. 数组中的特定对象具有必需属性
  3. 某些对象(来自array2),特别是array1具有必需的属性

诀窍是如何实现最后一个条件.


另一个例子:

{
    "attribute": "0",
    "array1": [{
        "id": "a12",
        "attribute": "1",
        "array2": [{
            "id": "a21_1",
            "attribute_1": "21_1"
        },{
            "id": "a21_2", …
Run Code Online (Sandbox Code Playgroud)

arrays postgresql json jsonb

3
推荐指数
1
解决办法
963
查看次数

标签 统计

arrays ×1

json ×1

jsonb ×1

postgresql ×1