Ali
Ali

Reputation: 267067

Is it possible to do this query in mysql?

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?

Upvotes: 3

Views: 113

Answers (3)

Rodney
Rodney

Reputation: 624

Basically new code would be:

$query = "select * from students"; 
$result = mysql_db_query ($dbname, $query, $link); 
while($row = mysql_fetch_array($result)) {
   $id = $row[0]; # corresponding to row in students database
   $firstName= $row[1]; # corresponding to row in students database
   $lastName = $row[2]; # corresponding to row in students database
   $email = $row[3]; # corresponding to row in students database
   echo 'id: '.$id;
   echo '<br>';
   echo 'firstName: '.$firstName;
   echo ' ';
   echo 'lastName: '.$lastName;
   echo '<br>';
   echo 'email: '.$email;
   echo '<br>';
   $query = "select * from courses where id  = '".$id."'"; 
   $result2 = mysql_db_query ($dbname, $query, $link); 
   while($row2 = mysql_fetch_array($result2)) {
     echo 'name: '.$row2[1]; # corresponding to row in courses database
     echo '<br>';
     echo 'price: '.$row2[2]; # corresponding to row in courses database
     echo '<br>';
   }
}

Upvotes: 2

Rodney
Rodney

Reputation: 624

$query = "select * from students inner join courses on studentID = '5' where something  = 'something'"; 
$result = mysql_db_query ($dbname, $query, $link); 
$id = $row[0]; # corresponding to row in students database
$firstName= $row[1]; # corresponding to row in students database
$lastName = $row[2]; # corresponding to row in students database
$email = $row[3]; # corresponding to row in students database
while($row = mysql_fetch_array($result)) {
  echo 'id: '.$row[id];
  echo '<br>';
  echo 'name: '.$row[name];
  echo '<br>';
  echo 'price: '.$row[price];
  echo '<br>';
}

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269693

You can do this by joining the tables together:

select s.*, c.*
from students s join
     courses c
     on s.studentid = c.studentid
where s.something = 'something'

If you want the student information on one row and the course on another, then it is not possible. All rows have to have the same columns.

Upvotes: 2

Related Questions