2014-03-26 94 views
0

首先,我知道MySQL是一種折舊語言,但我的主管堅持使用它。將多個表中的數據插入到一個單獨的表中

我在我的數據庫中有三個表。他們是:「學生」,「導師」,「匹配」。當我找到運行下面的查詢($ query2)時,我正在檢查確保該查詢中的所有條件都完全匹配。

當完美匹配我想插入以下到匹配表:

"insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')"; 

的「匹配」表中包含列:

match_id(auto incremented), tutor_id, student_id, school_id 

任何幫助將不勝感激。

if(mysql_query($query2)){ 
    $check_availability = "select * 
          from tutors, students 
          where tutor_availability = '$student_availability' 
          AND (tutor_subject_1 = '$student_subject_1' 
          OR tutor_subject_1 = '$student_subject_2' 
          OR tutor_subject_2 = '$student_subject_2' 
          OR tutor_subject_2 = '$student_subject_1')";  

    $run_5 = mysql_query($check_availability); 

    if(mysql_num_rows($run_5)>0) { 

     echo "<script>alert('we have a match')</script>"; 
     my_sql_query($query3) = "insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')"; 

     mysql_query($query3); 
    } else{ 
     echo "<script>alert('no match found')</script>"; 
    } 
} 
+0

MySQL是絕不棄用的語言,PHP mysql_ *功能 –

+0

那麼什麼是您的問題...? – user1844933

+0

這些值不會進入「匹配」表 – bano590

回答

0

我覺得這條線是錯誤的:

my_sql_query($query3) = "insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')"; 

函數名稱拼寫錯誤,參數不應該是$query3但實際查詢:

mysql_query("insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')"); 
+0

對不起,這是一個小錯誤。更正了它,但它仍然不起作用 – bano590

0

基本上你將前一個查詢的輸出插入到數據庫中。

變化

$check_availability = "select * 
         from tutors, students 
         where tutor_availability = '$student_availability' 
         AND (tutor_subject_1 = '$student_subject_1' 
         OR tutor_subject_1 = '$student_subject_2' 
         OR tutor_subject_2 = '$student_subject_2' 
         OR tutor_subject_2 = '$student_subject_1')"; 

$check_availability = "insert into match (tutor_id, student_id, school_id) 
         select tutors.id, students.id, students.school_id 
         from tutors, students 
         where tutor_availability = '$student_availability' 
         AND (tutor_subject_1 = '$student_subject_1' 
         OR tutor_subject_1 = '$student_subject_2' 
         OR tutor_subject_2 = '$student_subject_2' 
         OR tutor_subject_2 = '$student_subject_1')"; 
+0

有了這段代碼,由於某種原因它不再找到匹配項?我認爲它肯定會工作 – bano590

+0

@ bano590 - 這是一個插入查詢,而不是一個選擇。如果你想檢查匹配,那麼你將不得不運行你的選擇,然後遍歷行,然後插入。 –

+0

這種情況下最好的方法是什麼?我對MySQL – bano590

0

您可以通過該行必須循環得到插入值:

if(mysql_query($query2)){ 
    $check_availability = "select * 
          from tutors, students 
          where tutor_availability = '$student_availability' 
          AND (tutor_subject_1 = '$student_subject_1' 
          OR tutor_subject_1 = '$student_subject_2' 
          OR tutor_subject_2 = '$student_subject_2' 
          OR tutor_subject_2 = '$student_subject_1')";  

    $run_5 = mysql_query($check_availability); 

    if(mysql_num_rows($run_5)>0) { 

     echo "<script>alert('we have a match')</script>"; 
     while($row = mysql_fetch_assoc($run_5)) { 
      $tutor_id = $row['tutor_id']; 
      $student_id = $row['student_id']; 
      $school_id = $row['school_id']; 
      mysql_query("insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')"); 
     } 

    } else{ 
     echo "<script>alert('no match found')</script>"; 
    } 
} 

我不知道確切的字段名,但它應該指向正確的方向。

+0

相當陌生在這種情況下查詢$ res是什麼? – bano590

+0

@ bano590 $ res應該是$ run_5。對不起,我在示例代碼中糾正了這個問題。 – Typoheads

+0

你實際上得到了所有的字段名稱,但它仍然不起作用 – bano590

0

也許嘗試使用mysql_fetch_row($ run_5)並將選擇查詢更改爲只拉出tutor_id,student_id和school_id。

if(mysql_query($query2)){ 
$check_availability = "select t.tutor_id, s.student_id, t.school_id 
         from t.tutors, s.students 
         where t.tutor_availability = '$student_availability' 
         AND (t.tutor_subject_1 = '$student_subject_1' 
         OR t.tutor_subject_1 = '$student_subject_2' 
         OR t.tutor_subject_2 = '$student_subject_2' 
         OR t.tutor_subject_2 = '$student_subject_1')";  

$run_5 = mysql_query($check_availability); 

if(mysql_num_rows($run_5)>0) { 

    echo "<script>alert('we have a match')</script>"; 
    while($row = mysql_fetch_row($run_5)) { 
     $tutor_id = $row[0]; 
     $student_id = $row[1]; 
     $school_id = $row[2]; 
     mysql_query("insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')"); 
    } 

} else{ 
    echo "<script>alert('no match found')</script>"; 
} 

}

+0

我試過這段代碼,它是最接近我想要的答案。問題似乎是這樣:mysql_query(「insert into match(tutor_id,student_id,school_id)values('$ tutor_id','$ student_id','$ school_id')」); – bano590

相關問題