I have a MySQL database that contains the following tables:
Student table that looks like that:
|------------------------------------|
| studentId | studentName | school |
|------------------------------------|
| 1 | Student A | 1 |
| 2 | Student B | 2 |
| 3 | Student C | 2 |
|------------------------------------|
And a School table:
|------------------------------|
| schoolId | schoolName | desc |
|------------------------------|
| 1 | School A | ... |
| 2 | School B | ... |
| 3 | School C | ... |
|------------------------------|
Using PHP, I'm trying to fetch an array of all students and include their school information in a sub array.
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] => ...
)
)
)
I was able to achieve that by doing this
$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);
But I don't think this way is the most efficient for a large database since I'm looping through all the students and executing a query to get school row.
Is there anyway I can do all that by using only SQL?
JOINto get the school info and the student info together in the same row. Then just loop through like you are now and perform a similar manipulation to create the sub-array.