mysql查json多层_MYSQL查询返回JSON格式的字符串(多层嵌套)

简单的JSON格式请参看这篇博客:(https://blog.csdn.net/qq_39706570/article/details/104021291),本篇博客介绍复杂的嵌套方法。

表数据:

mysql查json多层_MYSQL查询返回JSON格式的字符串(多层嵌套)_第1张图片

查询展示效果:

{

"department_no": [

{

"001": [

{"department_name": "诊疗部","doctor_name": "黄裳","doctor_no": "100011"},

{"department_name": "诊疗部","doctor_name": "花满楼","doctor_no": "100012"}

]

},

{

"002": [

{"department_name": "护理部","doctor_name": "风清扬","doctor_no": "100013"},

{"department_name": "护理部","doctor_name": "杨清风","doctor_no": "100014"}

]

}

]

}

查询方式:

SELECT CONCAT("{\"department_no\":[",GROUP_CONCAT(a.msg),"]}") FROM

(

SELECT

CONCAT("{\"",department_no,"\":"

"[",

GROUP_CONCAT(

"{"

"\"department_name\":\"",department_name,"\",",

"\"doctor_name\":\"",doctor_name,"\",",

"\"doctor_no\":\"",doctor_no,"\"","}"

),"]}"

) msg

FROM basic_hospital_information GROUP BY department_no

)a

注意:以上SQL查询方法是使用了

GROUP_CONCAT()

该函数有长度限制,一般默认长度为1024,可以使用这篇博客(https://blog.csdn.net/qq_39706570/article/details/103999965)轻松修改长度范围。

备注:小编写这篇博客不易,希望大家能给我点个赞,谢谢。

你可能感兴趣的:(mysql查json多层)