2012-08-25 113 views
0

您好目前我有4個表在我的數據庫中:哪些是tb_student,tb_history,tb_section和tb_adviser。到目前爲止,我用這條線給我看給定學生的歷史:SQL語句4表連接

$qry_display = "SELECT 
     a.student_id, a.section_id, a.level, a.photo, a.address, a.father_occupation, a.father_phone, a.father_company, a.mother_occupation, a.mother_phone, a.mother_company,a.gpa, 
     b.fname, b.sex, b.lname, b.mname, b.birth_date, b.birth_place, b.address, b.father, b.father_degree, b.mother, b.mother_degree, 
     c.section_name, c.adviser_id 
     FROM tbl_er AS a 
     LEFT OUTER JOIN tbl_enroll AS b ON a.student_id = b.student_id 
     LEFT OUTER JOIN tbl_section AS c ON a.section_id = c.section_id 
     WHERE a.student_id=".$id." AND a.level='1st Year'"; 

我的主要問題是,現在我需要顯示這些其它信息顧問的姓氏。所以我在考慮放棄。需要注意的是tb_adviser只能通過adviser_id

 LEFT OUTER JOIN tbl_adviser AS d ON a.student_id = c.adviser_id 

連接到tb_section我加入這行之前有where語句。並會在我的SELECT字段中插入這一行。

 d.lname_adviser 

目前它不起作用。任何人都會對我的問題有所瞭解。

回答

1

眼看DB結構會有所幫助,但它看起來像你想在兩個不匹配的列上加入JOIN。 a.student_id是否會匹配c.adviser_id?

假設advisor_id是ID在tbl_advisor然後只需添加

LEFT OUTER JOIN tbl_advisor AS d 
ON d.id = c.advisor_id 
+0

是的,謝謝它的作品。 –

0

試試這個:

我認爲你應該做

LEFT OUTER JOIN tbl_adviser AS d ON d.adviser_id = c.adviser_id 

所以,你的查詢將是:

Select ..... 
FROM tbl_er AS a 
LEFT OUTER JOIN tbl_enroll AS b ON a.student_id = b.student_id 
LEFT OUTER JOIN tbl_section AS c ON a.section_id = c.section_id 
LEFT OUTER JOIN tbl_adviser AS d ON d.adviser_id = c.adviser_id 
WHERE a.student_id=".$id." AND a.level='1st Year' 
+0

謝謝你,它的工作完美。 –