将多个表中的数据插入一个单独的表中

时间:2014-03-26 10:19:28

标签: php mysql sql

首先,我知道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>";
    }
}

4 个答案:

答案 0 :(得分: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')");

答案 1 :(得分: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')";  

答案 2 :(得分: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>";
    }
}

我不知道确切的字段名称,但它应该指向正确的方向。

答案 3 :(得分: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>";
}

}