0

您好,目前我的数据库中有 4 个表:tb_student、tb_history、tb_section 和 tb_adviser。到目前为止,我用这条线向我展示了一个给定学生的历史:

   $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 仅通过 advisor_id 连接到 tb_section

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

我在 where 语句之前添加了这一行。并将这一行插入到我的 SELECT 字段中。

     d.lname_adviser

目前它不起作用。任何人都会对我的问题有所了解。

4

2 回答 2

1

查看 db 结构会有所帮助,但看起来您正试图在两个不匹配的列上加入 JOIN。a.student_id 会匹配 c.adviser_id 吗?

假设 advisor_id 是 tbl_advisor 中的 id 然后只需添加

 LEFT OUTER JOIN tbl_advisor AS d 
 ON d.id = c.advisor_id
于 2012-08-25T03:14:15.233 回答
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'
于 2012-08-25T03:11:41.070 回答