我有一个给我的查询。我想使用php脚本执行此操作,单击按钮并将其插入另一个表。
以下是查询:
SELECT students.student_id,GROUP_CONCAT(teachers.teacher_name) FROM
students JOIN teachers ON teachers.subject_teaches =
students.subject_enrolled GROUP BY students.student_id
我有一个脚本,显示它的内容并且它正在工作。我所要做的就是将它插入另一个表。我该怎么做?
<?php
mysql_connect("localhost","root");
mysql_select_db("join");
$res=mysql_query("SELECT students.student_id, GROUP_CONCAT(teachers.teacher_name)FROM students
JOIN teachers ON teachers.subject_teaches = students.subject_enrolled
GROUP BY students.student_id");
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><?php echo $row['GROUP_CONCAT(teachers.teacher_name)'];?></td><br>
</tr>
<?php
}
?>
答案 0 :(得分:0)
如果您真的只想将结果集的数据插入到另一个表中,则只需使用:http://dev.mysql.com/doc/refman/5.7/en/insert-select.html
querysyntax:
INSERT INTO insertTable (columnsOfTable) SELECT columnsToInsert FROM selectTable;
在你的情况下:
$res = mysql_query("INSERT INTO yourtable (columnStudent_id, columnTeachernames) SELECT students.student_id, GROUP_CONCAT(teachers.teacher_name)FROM students
JOIN teachers ON teachers.subject_teaches = students.subject_enrolled
GROUP BY students.student_id");