MySQL将2个相关表提取到子数组中

时间:2016-11-18 15:06:49

标签: php mysql

我有一个MySQL数据库,其中包含以下表格:

Student表看起来像这样:

|------------------------------------|
| studentId | studentName | school   |
|------------------------------------|
| 1         | Student A   | 1        |
| 2         | Student B   | 2        |
| 3         | Student C   | 2        |
|------------------------------------|

School表:

|------------------------------|
| schoolId | schoolName | desc |
|------------------------------|
| 1        | School A   | ...  |
| 2        | School B   | ...  |
| 3        | School C   | ...  |
|------------------------------|

使用PHP,我试图获取所有学生的数组,并将他们的学校信息包含在子数组中。

Array
(
    [0] => Array
        (
            [studentId] => 1
            [studentName] => Student A
            [school] => Array
                (
                    [schoolId] => 1
                    [schoolName] => School A
                    [desc] => ...
                )
        )
    [1] => Array
        (
            [studentId] => 1
            [studentName] => Student A
            [school] => Array
                (
                    [schoolId] => 1
                    [schoolName] => School A
                    [desc] => ...
                )
        )
    [2] => Array
        (
            [studentId] => 1
            [studentName] => Student A
            [school] => Array
                (
                    [schoolId] => 1
                    [schoolName] => School A
                    [desc] => ...
                )
        )
)

通过这样做我能够实现这个目标

$studentsResult = $conn->query("SELECT * FROM STUDENT");
$studentsArray = $studentsResult->fetch_all(MYSQLI_ASSOC);

$finalArray = array();

foreach ($studentsArray as &$student) { 
    $schoolSQL = "SELECT * FROM SCHOOL WHERE schoolId = ".$student['schoolId'];
    $schoolResult = $conn->query($schoolSQL);
    $schoolArray = $schoolResult->fetch_all(MYSQLI_ASSOC);

    unset($student['schoolId']);
    $student['school'] = $schoolArray[0];
    $finalArray[] = $student;
}
echo '<pre>'; print_r($finalArray);

但我不认为这种方式对于大型数据库来说是最有效的,因为我循环遍历所有学生并执行查询以获得学校排。

无论如何我只能使用SQL来完成所有这些工作吗?

2 个答案:

答案 0 :(得分:1)

你应该阅读加入,非常简单易用。

SELECT a.studentName, b.schoolName, b.desc
FROM students AS a
INNER JOIN school AS b
ON a.school = b.schoolId

这样,您可以将来自多个表的所有数据合并为1个结果。

echo $row['studentName'], ' is in this school ', $row['schoolName'], ' etc..';

答案 1 :(得分:1)

进行加入,然后在学生更改时仅循环添加学生。但只需将学校详细信息添加到学生学校阵列中。

像这样的东西

<?php

$studentsResult = $conn->query("SELECT a.studentId,
                                        a.studentName,
                                        b.schoolId,
                                        b.schoolName,
                                        b.desc
                                FROM STUDENT a
                                LEFT OUTER JOIN SCHOOL b
                                ON a.schoolId = b.schoolId");

$prev_student = 0;
$studentsArray = $studentsResult->fetch_all(MYSQLI_ASSOC);

$finalArray = array();

foreach ($studentsArray as &$student) 
{ 
    if ($prev_student != $student['studentId'])
    {
        $finalArray[] = array('studentId' => $student['studentId']
                                'studentName' => $student['studentName']
                                'school' => array());
        $prev_student = $student['studentId'];
    }
    $finalArray[key($finalArray)][] = array('schoolId ' => $student['schoolId']
                                            'schoolName ' => $student['schoolName']
                                            'desc ' => $student['desc']);
}

echo '<pre>'; print_r($finalArray);