显示多个表SQL / PHP的结果

时间:2012-11-08 05:05:35

标签: php mysql

我目前有一个包含12个表的数据库。我正在做一个php查询从数据库中提取信息,但我没有显示任何内容。查询单独桥接所有以表schedule开头的表,这些表具有相关的外键。我是否需要从表class开始查询并与其他表桥接?

TABLE DESIGN- PICTURE

如果您想复制我的设计 - QUERY

$query = ("SELECT  class_name, class_caption, class_credit_hours, class_description
                FROM schedule 
                INNER JOIN section 
                ON class.id = section.class_id
                INNER JOIN faculty
                ON faculty.id = section.faculty_id
                INNER JOIN faculty
                ON faculty.id = office_hours.faculty_id
                INNER JOIN faculty_titles
                ON faculty_titles.faculty_id = faculty.id
                INNER JOIN faculty_education
                ON faculty_education.faculty_id = faculty.id 
                INNER JOIN section
                ON section.faculty_id = faculty.id 
                INNER JOIN class
                ON class.id = section.class_id
                INNER JOIN major_class_br
                ON major_class_br.class_id = class.id
                INNER JOIN  major_minor 
                ON major_class_br.major_minor_id = major_minor.id                
                 ");
      //execute query
      $result = mysql_query($query);

     if ($result){

    $totalhours = 0;

    while ($row = mysql_fetch_assoc( $result ))
    {  
        print "<b>" . $row['class_name'] . "</b><br>";
        print $row['class_caption'] . "<br>";
        print $row['class_description'] . "<br>";
        print $row ['class_credit_hours'] . "hrs. <br>";
        print "------------------------------<br />";
        $totalhours += $row['class_credit_hours']; 
    }   
    }

SQL fiddle query

1 个答案:

答案 0 :(得分:2)

SELECT  class_name, class_caption, class_credit_hours, class_description
             FROM schedule 
            INNER JOIN section 
            ON class.id = section.class_id

就在这里有一个问题:你正在使用字段'class.id'进行INNER JOIN,但表'class'不在JOIN的任何一侧。所以它不会起作用。

查询应该像这样开始:

SELECT  class_name, class_caption, class_credit_hours, class_description
             FROM class
            INNER JOIN section 
            ON class.id = section.class_id

然后使用表'schedule'与表共享一个公共索引(我猜它会是类)。

完整的查询应该是这样的:

SELECT  class.class_name, class.class_caption, class.class_credit_hours, class.class_description
            FROM class
            INNER JOIN section 
            ON class.id = section.class_id
            INNER JOIN faculty 
            ON faculty.id = section.faculty_id OR faculty.id = office_hours.faculty_id
            INNER JOIN faculty_titles
            ON faculty_titles.faculty_id = faculty.id
            INNER JOIN faculty_education
            ON faculty_education.faculty_id = faculty.id 
            INNER JOIN major_class_br
            ON major_class_br.class_id = class.id
            INNER JOIN major_minor 
            ON major_class_br.major_minor_id = major_minor.id
            INNER JOIN sched_sect_br 
            ON sched_sect_br.section_id =  section.id
            INNER JOIN schedule
            ON schedule.id = sched_sect_br.schedule_id
            INNER JOIN semester
            ON semester.id = schedule.semester_id
            INNER JOIN office_hours
            ON schedule.id = office_hours.schedule_id AND faculty.id = office_hours.faculty_id

此查询从图表中的所有表中获取信息,除了事件表之外,它与任何其他表无关。但是这个查询仍然应该在SELECT上有更多的字段(你只是类表中的选择字段,我知道你也想要来自其他10个表的数据),这样就可以简单地将'tablename.field'添加到SELECT中的字段列表。