我希望SO会帮助我。在过去的几天里,我一直在尝试处理查询但没有得到正确的解决方案。 我的查询如下
$sql = "SELECT
m.MarkID, m.studentID,
sm.ExamName, sb.Subject,
sm.MaxMarks, m.MarksRecvd
FROM marks AS m
RIGHT JOIN student AS st
ON m.studentID=st.studentID
RIGHT JOIN semester AS sm
ON m.ExamID=sm.ExamID
RIGHT JOIN subject AS sb
ON m.SubjectID=sb.SubjectID
WHERE m.studentID = $studentID
ORDER BY ExamName ASC";
$sql = $con->prepare($sql);
$sql->execute();
$rows = array();
while ($row = $sql->fetch(PDO::FETCH_OBJ)) {
$rows['data'][] = $row;
}
echo json_encode($rows, JSON_NUMERIC_CHECK);
这导致json:
{"data":[
{"MarkID":22,"studentID":2,"ExamName":"Semester-1","Subject":"Sports","MaxMarks":100,"MarksRecvd":45},
{"MarkID":20,"studentID":2,"ExamName":"Semester-1","Subject":"Science","MaxMarks":100,"MarksRecvd":15},
{"MarkID":21,"studentID":2,"ExamName":"Semester-2","Subject":"Social Science","MaxMarks":100,"MarksRecvd":25}]}
并输出到表格如下:
但是需要帮助以获得如下所示的json响应:
{"data":
"studentID":2
[
{
"Subject":"Sports",
"Result":
[
{
"ExamName":"Semester-1",
"MaxMarks":100,
"MarksRecvd":15
},
{
"ExamName":"Semester-2",
"MaxMarks":100,
"MarksRecvd":29
}
]
"Subject":"Science",
"Result":
[
{
"ExamName":"test-1",
"MaxMarks":10,
"MarksRecvd":8
},
{
"ExamName":"Semester-2",
"MaxMarks":100,
"MarksRecvd":84
}
]
}
]}
如果可能,请使用json指向CRUD多对多数据库的好文章
我正在尝试复制如下表格: