【发布时间】:2022-01-11 07:14:16
【问题描述】:
我做了一个 DB Fiddle,看看这张桌子看起来像 https://www.db-fiddle.com/f/4jyoMCicNSZpjMt4jFYoz5/3382
表格中的数据如下所示
[
{
"id": 1,
"form_id": 1,
"questionnaire_response": [
{
"id": "1",
"title": "Are you alive?",
"value": "Yes",
"form_id": 0,
"shortTitle": "",
"description": ""
},
{
"id": "2",
"title": "Did you sleep good?",
"value": "No",
"form_id": 0,
"shortTitle": "",
"description": ""
},
{
"id": "3",
"title": "Whats favorite color(s)?",
"value": [
"Red",
"Blue"
],
"form_id": 0,
"shortTitle": "",
"description": ""
}
]
},
{
"id": 2,
"form_id": 1,
"questionnaire_response": [
{
"id": "1",
"title": "Are you alive?",
"value": "Yes",
"form_id": 0,
"shortTitle": "",
"description": ""
},
{
"id": "2",
"title": "Did you sleep good?",
"value": "Yes",
"form_id": 0,
"shortTitle": "",
"description": ""
},
{
"id": "3",
"title": "Whats favorite color(s)?",
"value": "Black",
"form_id": 0,
"shortTitle": "",
"description": ""
}
]
},
{
"id": 3,
"form_id": 1,
"questionnaire_response": [
{
"id": "1",
"title": "Are you alive?",
"value": "Yes",
"form_id": 0,
"shortTitle": "",
"description": ""
},
{
"id": "2",
"title": "Did you sleep good?",
"value": "No",
"form_id": 0,
"shortTitle": "",
"description": ""
},
{
"id": "3",
"title": "Whats favorite color(s)?",
"value": [
"Black",
"Red"
],
"form_id": 0,
"shortTitle": "",
"description": ""
}
]
}
]
我有一个问题select * from form_responses,jsonb_to_recordset(form_responses.questionnaire_response) as items(value text, id text) where (items.id = '3' AND items.value like '%Black%');
但不能像select * from form_responses,jsonb_to_recordset(form_responses.questionnaire_response) as items(value text, id text) where (items.id = '3' AND items.value like '%Black%') AND (items.id = '2' AND items.value like '%Yes%');那样做多个对象
对象中的值字段也可以是数组或单个值.. 不可预测.. 我觉得我很接近但也不确定我是否首先使用了正确的查询。
任何帮助将不胜感激!
编辑
select * from form_responses where(
questionnaire_response @> '[{"id": "2", "value":"No"},{"id": "3", "value":["Red"]}]')
似乎可行,但不确定这是否是最好的方法
【问题讨论】:
-
尝试构建一个过滤系统,我可以在其中动态传递多个 ID 和值并从中获取整个记录 [{id: 1, value: 'Yes'},{id: 2, value: '否'}]
标签: json postgresql jsonb