1

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

桌子设计 - 图片

如果你想复制我的设计-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小提琴查询

4

1 回答 1

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'与它共享一个公共索引的表进行JOIN(我猜它会是类)。

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

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 中的字段列表。

于 2012-11-08T11:25:54.483 回答