2012-11-09 60 views
1

我在做一個sql查詢,但它沒有從表中返回任何數據。我不知道爲什麼,我已經從INNER更改爲LEFT,但沒有運氣。我已經修改了幾次查詢,但我無法找到問題。任何想法爲什麼我沒有得到任何顯示?SQL JOIN查詢不顯示任何內容 - PHP

TABLE STRUCTURE

TABLE CREATE QUERY

PHP

$query = ("SELECT class.class_name, class.class_caption, class.class_credit_hours, class.class_description, faculty_fname, faculty_lname 
      FROM class 
      LEFT JOIN section 
      ON class.id = section.class_id 
      LEFT JOIN faculty 
      ON faculty.id = section.faculty_id OR faculty.id = office_hours.faculty_id 
      LEFT JOIN faculty_titles 
      ON faculty_titles.faculty_id = faculty.id 
      LEFT JOIN faculty_education 
      ON faculty_education.faculty_id = faculty.id 
      LEFT JOIN major_class_br 
      ON major_class_br.class_id = class.id 
      LEFT JOIN major_minor 
      ON major_class_br.major_minor_id = major_minor.id 
      LEFT JOIN sched_sect_br 
      ON sched_sect_br.section_id = section.id 
      LEFT JOIN schedule 
      ON schedule.id = sched_sect_br.schedule_id 
      LEFT JOIN semester 
      ON semester.id = schedule.semester_id 
      LEFT JOIN office_hours 
      ON schedule.id = office_hours.schedule_id AND faculty.id = office_hours.faculty_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_credit_hours'] . "hrs. <br>"; 
      print $row ['faculty_lname'] . "hrs. <br>"; 
      print $row ['faculty_fname'] . "hrs. <br>"; 
     print $row['class_description'] . "<br>";  
     print "------------------------------<br />"; 
     $totalhours += $row['class_credit_hours']; 
    } 
    } 

    print "<p>Total hours for Major: " . $totalhours . ".</p>"; 

所需的顯示:

Computer Programming I 
CP1000 
4 
James Doe 
This course offers introduction to programming. 

更新:這個問題是在這裏找到,但我不知道爲什麼

ON faculty.id = section.faculty_id OR faculty.id = office_hours.faculty_id 
+1

你的表都有必要的行,是嗎? – cdhowie

+0

您的'SELECT'列表僅從'class'和'faculty'繪製,並且您沒有'WHERE'子句。除了'class - > section - > faculty'之外,所有其他'JOIN'的原因是什麼? –

+0

在我看來,你的'班級'表是空的。 – Malvolio

回答

2
  • 複製SQL,去MySQL管理,執行SQL那裏,看看它是否返回任何結果。

  • 刪除所有連接,然後再執行,你應該看到至少一些

  • 添加JOIN逐一

  • 試圖找出其中的JOIN,導致沒有行返回

  • 檢查該連接的左側和右側,確保實際上有有效數據加入

或者使用OUTER JOIN