我的意图是检索包含Json对象数组中相关页面的页面部分列表。
例如:
[
{
"sectionid": "value1",
"sectiondescription": "value1",
"pages": [
{
"pageid": "id1",
"pagedescription": "desription1",
},
{
"pageid": "id2",
"pagedescription": "desription2",
}
]
},
{
"sectionid": "value2",
"sectiondescription": "value2",
"pages": [
{
"pageid": "id3",
"pagedescription": "desription3",
},
{
"pageid": "id4",
"pagedescription": "desription4",
}
]
}
]
我在Mysql中使用Nodejs。
我使用下面的mysql查询来检索为给定用户角色启用的部分:
SELECT section.SectionId,section.Description
FROM section as section
INNER JOIN userroletosection AS urts
ON section.SectionId = urts.SectionID
INNER JOIN user AS user
ON user.RoleID = urts.UserRoleID
WHERE
user.UserId = 1
此查询将检索给定部分的页面:
SELECT p.PageID,p.Description,p.URL
FROM page AS p
INNER JOIN sectiontopage AS stp
ON p.PageID = stp.PageID
WHERE
stp.SectionID = ?
我需要知道如何使用循环运行查询以及如何使用nodejs和mysql构造Json数组对象。
我在这里附加了nodejs代码
conn.query('SELECT *从作为节的部分INNER JOIN userroletosection AS urts ON section.SectionId = urts.SectionID INNER JOIN用户作为user ON user.RoleID = urts.UserRoleID WHERE user.UserId = 1',(err ,rows,fileds)=> {
if(!err)
{
for (var i = 0;i < rows.length; i++) {
conn.query('SELECT p.PageID,p.Description,p.URL FROM page AS p INNER JOIN sectiontopage AS stp ON p.PageID = stp.PageID WHERE stp.SectionID = ?',[rows[i].SectionId],(err2,rows2,fileds)=>{
if(!err2)
{
for(var j =0; j<rows2.length;j++)
{
obj2.push({ PID: JSON.stringify(rows2[j].PageID),
PageID: rows2[j].PageID,
PageDescription: rows2[j].Description,
PageUrl: rows2[j].URL
})
}
console.log(obj2);
}
})
{
objs.push({ID: JSON.stringify(rows[i].SectionId),
ID: rows[i].SectionId,
sectionDesciption: rows[i].Description,
Pages: obj2
});
}
}
答案 0 :(得分:0)
您将必须使用MySQL JSON函数才能访问JSON文档中的项目,例如JSON_EXTRACT和JSON_CONTAINS