创建一个产生嵌套JSON数组对象的MySQL过程
我需要在mysql中创建一个存储过程,从DB中获取一些字段,并创建一个整洁的json对象: 首先,我创建一个json对象,如下所示:创建一个产生嵌套JSON数组对象的MySQL过程,mysql,json,object,nested,Mysql,Json,Object,Nested,我需要在mysql中创建一个存储过程,从DB中获取一些字段,并创建一个整洁的json对象: 首先,我创建一个json对象,如下所示: { "form": "Exams tests", "version": "v3.001.01", "questions": [] } 其次是一个json数组对象,如下所示: {[ { "ordem": 1, "num_questions": 1, "q
{
"form": "Exams tests",
"version": "v3.001.01",
"questions": []
}
其次是一个json数组对象,如下所示:
{[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
{
"form": "Exams tests",
"version": "v3.001.01",
"questions": {[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
}
结果查询如下所示:
{[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
{
"form": "Exams tests",
"version": "v3.001.01",
"questions": {[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
}
我在尝试将嵌套json数组插入json对象时出错我将json_对象与json_ArrayAg连接在一起,如下所示:
{
"name": "Moon",
"resume": "This is a resume.",
"dt_ini": "2018-09-01",
"dt_end": null,
"cases": [
{
"unit": "unit 1",
"unit_id": 10
},
{
"unit": "unit 2",
"unit_id": 290
},
{
"unit": "unit 3",
"unit_id": 44
},
{
"unit": "unit 4",
"unit_id": 108
}
]
}
最终结果是:
CREATE DEFINER=`root`@`localhost` PROCEDURE `get_list_case`(
IN `code` int,
IN `base_code` int)
BEGIN
DECLARE json TEXT DEFAULT '';
SELECT JSON_OBJECT(
'name', v.name,
'dt_ini', v.dt_ini,
'dt_end', v.dt_end,
'resumo', v.resumo,
'cases', ( select json_arrayagg(json_object(
'unit_id',`tb_unit`.`unit_id`,
'unit',`tb_unit`.`unit`))
from tb_unit_case
INNER JOIN tb_unit ON tb_unit_case.unid_code = tb_unit.unit_id
WHERE tb_unit_case.case_code = code)
) INTO json
FROM v_case AS v
WHERE v.code = code and v.base_code = base_code;
SELECT json;
END
请让我们看看代码,一封信很好,但你最好给我们看代码。你可以在这里找到答案:这是否回答了你的问题?我现在明白了。我将JSON_ArrayAg与JSON_对象一起使用。只需一次选择即可完成所有步骤,如下所示:选择JSON_对象“form”,v.form_名称,“version”,v.version,“questions”,选择JSON_ArrayAgjson_对象“ordem”,tb_questions.order,'num_questions',tb_questions.num'question1',tb_questions.question1'answer1',tb_questions.answer1从tb_questions转换成json从v_case作为v;