Say I have a table called students, containing a list of all the students. Each student can have many courses stored in another table, identified by a student id.
I want to do a query which will get the row from the students table with the info of the student, and also get all of his courses, in one query. The output I want is something like this:
Array
(
[0] => Array
(
[id] => 5
[firstName] => Bob
[lastName] => Smith
[email] => ....
[courses] => Array
(
[0] => Array
(
[id] => 30
[name] => Test Course
[price] => 400
)
[1] => Array
(
[id] => 30
[name] => Test Course 2
[price] => 300
)
)
)
)
To get the info of students, I would do this query:
SELECT * FROM students WHERE something ='something'
To get the courses for a student, I would do this:
SELECT * FROM courses WHERE studentId = '5'
Is it possible to do both queries at once and get an output like the array above, or do I have to loop through each student and get its courses, then attach it to the array manuallly?
1 Answer