Php 一个JSON包含多个查询

Php 一个JSON包含多个查询,php,mysql,json,delphi,delphi-2007,Php,Mysql,Json,Delphi,Delphi 2007,大家更新我的问题:我有一个Delphi 2007函数“ImportStudentMisses”: 此函数用于导入在各自的一天中缺课的所有学生。我必须让这个函数变成JSON,如下所示: [{"ClassID":10,"StudentsID":[1,2]},{"ClassID":20,"StudentsID":[3,4]}] 正如您所看到的,JSON包含(“ClassID”:10)和缺少该类的学生列表(“学生”:[1,2,3,4,5])。我已经尝试了下面的代码来制作我想要的JSON $query

大家更新我的问题:我有一个Delphi 2007函数“ImportStudentMisses”:

此函数用于导入在各自的一天中缺课的所有学生。我必须让这个函数变成JSON,如下所示:

[{"ClassID":10,"StudentsID":[1,2]},{"ClassID":20,"StudentsID":[3,4]}]
正如您所看到的,JSON包含(“ClassID”:10)和缺少该类的学生列表(“学生”:[1,2,3,4,5])。我已经尝试了下面的代码来制作我想要的JSON

$query = $database->query( "SELECT ClassID, GROUP_CONCAT(StudentID) AS StudentID FROM {$pfx}MissOneClass
GROUP BY ClassID ORDER BY ClassID");
$list = array();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    $list[] = array('ClassID' => $row['ClassID'], 
    'StudentID' => array_map('intval', explode(',', $row['StudentID'])));
    }
echo json_encode($list);
我得到了这个JSON:

[{"ClassID":1,"StudentsID":[1,2,3,4]},{"ClassID":2,"StudentsID":[3,4,23]}]
JSON就是我需要的JSON。但有人告诉我,我必须使用所有的表格,而不是像我那样只使用“MissOneClass”

此JSON将由JAVA应用程序使用,该应用程序将在数据库中插入JSON数据。这就是角色问题


谢谢你

在Union中,您使用的所有3个表中的列数应该相同。检查union和union all之间的差异。在数据库上运行查询以检查结果

SELECT ClassID, StudentID, null as Date, null as DisciplineID
FROM MissOneClass
union all
SELECT null as ClassID,StudentID, Date, DisciplineID
FROM MissTwoClasses
union all
SELECT null as ClassID, StudentID, Date, null as DisciplineID
FROM MissTheDay
ORDER BY Date, DisciplineID, ClassID, StudentID
您也可以使用join,但仍然不清楚您希望以何种方式合并JSON,这2种方式将产生不同的结果。在这种情况下,我假设学生id是外键

SELECT ClassID, StudentID, Date, DisciplineID
FROM MissOneClass mo full outer join MissTwoClasses mt on mt.StudentID = mo.StudentID full outer join MissTheDay md md.StudentID = mo.StudentID

为什么不能与其他查询合并?是否要合并这3个查询?在你的JSON中?或者你可以选择使用join?我需要这三个查询成为一个JSON@SayedMohdAli,不管如何。因此,是的,我可以选择使用join,但我不擅长使用mysqlyet@PeterCoast然后,您可以在SQL中使用union合并3个查询的结果,然后可以对结果进行json_编码。或者,您可以运行3个查询并生成所有3个查询的json,然后使用=>jsonArray1.concat(jsonArray2)将json对象concate;
SELECT ClassID, StudentID, Date, DisciplineID
FROM MissOneClass mo full outer join MissTwoClasses mt on mt.StudentID = mo.StudentID full outer join MissTheDay md md.StudentID = mo.StudentID