简体   繁体   English

object 的对象查询使用 postgresql

[英]object of objects query using postgresql

i am using postgresql我正在使用 postgresql

in profile model there is field (progress ) which is an object and inside this object there are multiple objects在配置文件 model 中有一个字段(进度),它是一个 object 并且在这个 object 中有多个对象

each object has 2 number每个object有2个号码

i want to check if those 2 numbers are equal to do increase points我想检查这 2 个数字是否等于做加分

progress : {
    "progress1": {
        "mustWin": 1,
        "progress": 0
    },
    "progress2": {
        "mustWin": 1,
        "progress": 0
    },
}

this is the default value of progress这是进度的默认值

how can i get access to progress1 and 2我怎样才能访问 progress1 和 2

If you want to manually gets progress1 and progress2 values, then use this:如果你想手动获取 progress1 和 progress2 值,那么使用这个:

with tb as (
select 
'{"progress" : {
    "progress1": {
        "mustWin": 1,
        "progress": 0
    },
    "progress2": {
        "mustWin": 1,
        "progress": 0
    } 
}}'::jsonb as js 
)
select 
    tb.js->'progress'->'progress1', 
    tb.js->'progress'->'progress2'  
from tb 

-- Result:
|progress1                    |progress2                    |
|-----------------------------+-----------------------------+
|{"mustWin": 1, "progress": 0}|{"mustWin": 1, "progress": 0}|

If you don't know how many objects have inside progress, then use this:如果你不知道有多少对象有内部进度,那么使用这个:

with tb as (
select 
'{"progress" : {
    "progress1": {
        "mustWin": 1,
        "progress": 0
    },
    "progress2": {
        "mustWin": 1,
        "progress": 0
    }, 
    "progress3": {
        "mustWin": 1,
        "progress": 0
    }, 
    "progress4": {
        "mustWin": 1,
        "progress": 0
    } 
}}'::jsonb as js 
)
select tb2."key", tb2."value" from tb tb1 
cross join jsonb_each_text(tb1.js->'progress') tb2  

-- Result:
| key       | value                         |
|-----------|-------------------------------|
| progress1 | {"mustWin": 1, "progress": 0} |
| progress2 | {"mustWin": 1, "progress": 0} |
| progress3 | {"mustWin": 1, "progress": 0} |
| progress4 | {"mustWin": 1, "progress": 0} |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM