我有2个SQL表,表Parent和child…。
我想进行子查询或联合或联接(没有区别),以便在这样的嵌套json数据中显示此父子表(在我的情况下为nodeJs)
父表:
ParentId ParentName ParentDescription
---------------------------------------------------------
1 | ParentName1 | ParentDescription1
2 | ParentName2 | ParentDescription2
3 | ParentName3 | ParentDescription3
4 | ParentName4 | ParentDescription4
子表:
childId childName childDescription ParentId(foreign key)
---------------------------------------------------------
1 | childName1 | childDescription1 1
2 | childName2 | childDescription2 1
3 | childName3 | childDescription3 1
4 | childName4 | childDescription4 1
5 | childName5 | childDescription5 2
6 | childName6 | childDescription6 2
7 | childName7 | childDescription7 2
8 | childName8 | childDescription8 2
我想显示这样的json嵌套数据…,,我知道如何在后端、、、 ..中使用json stringfy实现此数据。我的问题是如何进行子查询或递归sql查询来做到这一点
[
{
"parentId": 1,
"parentName": " ParentName1 ",
"ParentDescription": "ParentDescription1 " ,
[
{ "childId": 1,
"childName": "childName1",
"childDescription": "childDescription1",
},
{ "childId": 2,
"childName": "childName2",
"childDescription": "childDescription2",
},
{ "childId": 3,
"childName": "childName3",
"childDescription": "childDescription3",
},
{ "childId": 4,
"childName": "childName3",
"childDescription": "childDescription3",
}
]
},
{
"parentId": 2,
"parentName": " ParentName2 ",
"ParentDescription": "ParentDescription2 " ,
[
{ "childId": 5,
"childName": "childName5",
"childDescription": "childDescription1",
},
{ "childId": 6,
"childName": "childName6",
"childDescription": "childDescription2",
},
{ "childId": 7,
"childName": "childName7",
"childDescription": "childDescription3",
},
{ "childId": 8,
"childName": "childName8",
"childDescription": "childDescription3",
}
]
},
…
…
…
…
…
...
]