Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining sql queries in PHP

Tags:

sql

join

php

mysql

Hello currently working with this code

   $qry_display = "
SELECT 
    student_id,
    fname,
    sex,
    lname,
    mname,
    level,
    photo,
    birth_date,
    birth_place,
    address,
    father,
    father_occupation,
    father_phone,
    father_company,
    father_degree,
    mother,
    mother_occupation,
    mother_phone,
    mother_company,
    mother_degree,
    adviser_id 
from 
    tbl_enroll 
where 
    student_id='$id' 
    AND level='$lvl'

";
    $sql_display = mysql_query($qry_display) or die (mysql_error());

The above code fetches most data from tbl_enroll. Now I want to acquire some data on tbl_er. tbl_enroll and tbl_er are connected with the primary key of student_id , also connect once to tbl_section. tbl_er and tbl_section are connected with the foreign key of section_id.

So far I was thinking of doing multiple sql queries and use one mysql_query trigger but it wont work because the trigger wont work with three sql queries.

like image 902
Anton Avatar asked Sep 05 '12 11:09

Anton


1 Answers

Just JOIN the three tables:

SELECT t1.*, t2.*, t3.*
from tbl_enroll t1
JOIN tbl_el t2 ON t1.student_id = t2.student_id
JOIN tbl_section t3 ON t2.section_id = t3.section_id
where student_id='$id' AND a.level='$lvl'
like image 114
Mahmoud Gamal Avatar answered Sep 18 '22 23:09

Mahmoud Gamal