Combining sql queries in PHP

Hi, 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 retrieves most of the data from tbl_enroll . Now I want to get some data about tbl_er . tbl_enroll and tbl_er are associated with the student_id primary key , also connect to tbl_section once . tbl_er and tbl_section are associated with the section_id foreign key.

So far, I have been thinking of making multiple sql queries and using one mysql_query trigger, but it will not work because the trigger will not work with three sql queries.

+5
source share
5

JOIN :

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'
+8

join . php, sql.

+6

, systax

select tb1.filds,tb2.fields ... from table1 as tb1, table2 as tb2 .. where tb1.id = tb2.id and tb2.id  = tb3.id ...

In those cases when the condition should be checked correctly.

else you can use the "Join Query" .. Join Query is better at

+3
source
   SELECT te.XXX,tr.YYYY,ts.ZZZ, ..... 
   from tbl_enroll te 
   inner join tbl_er tr on tr.student_id = te.student_id
   inner join tbl_section ts on ts.section_id = te.section_id
   where student_id='$id' and level='$lvl';

You select any field from te, tr or ts at the beginning of your query. Do not forget the prefix with te, tr or ts fields, so that there are no ambiguous references when executing the request;)

+2
source
Combining two queries..

SELECT t1.* FROM tbl_er as t1, tbl_enroll as t2
WHERE t1.student_id= t2.student_id ORDER BY id DESC

You must define the student ID in both tables.

+1
source

All Articles