[英]inserting data from multiple tables into one separate table
First of all, I know MySQL is a depreciated language, but my supervisor insists I use it. 首先,我知道MySQL是一种折旧的语言,但是我的主管坚持要使用它。
I have three tables in my database. 我的数据库中有三个表。 They are : "students", "tutors", "match" . 他们是: “学生”,“导师”,“比赛” 。 When I find a run the below query($query2) I am checking the make sure all the criteria in that query is perfectly matched. 当我找到以下查询($ query2)的运行时,我正在检查以确保该查询中的所有条件都完全匹配。
When it is perfectly matched I want to insert the following into the match table: 当它完全匹配时,我想在匹配表中插入以下内容:
"insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')";
The "match" table contains the columns: “匹配”表包含以下列:
match_id(auto incremented), tutor_id, student_id, school_id
Any help would be greatly appreciated. 任何帮助将不胜感激。
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>";
}
}
I think this line is wrong: 我认为这行是错误的:
my_sql_query($query3) = "insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')";
The function name is spelled wrong and the parameter shouldn't be $query3
but the actual query: 函数名称拼写错误,并且参数不应为$query3
而是实际的查询:
mysql_query("insert into match (tutor_id, student_id, school_id) values ('$tutor_id','$student_id','$school_id')");
Basically you have to insert the output of the previous query into the database. 基本上,您必须将上一个查询的输出插入数据库。
change 更改
$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')";
to 至
$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')";
You have to loop through the row to get the values to insert: 您必须遍历该行以获取要插入的值:
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>";
}
}
I don't know the exact field names, but it should point you in the right direction. 我不知道确切的字段名称,但它应该为您指明正确的方向。
Maybe try using mysql_fetch_row($run_5) and change the select query to only pull out tutor_id, student_id and school_id. 也许尝试使用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>";
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.