我有一个由MySQL表填充的表,我试图将表中的选定行插入到另一个表中。
表中前三个字段来自另外两个表,具体取决于字段值。
下拉选项由用户使用:
提供$sql = "SELECT project_proposals.id, project_proposals.title, project_proposals.description, project_proposals.academicname, flux_student_records.studentname, flux_student_records.id, flux_student_records.programme, flux_student_records.academicdiscipline FROM project_proposals JOIN flux_student_records ON project_proposals.academicdiscipline = flux_student_records.academicdiscipline
WHERE project_proposals.academicdiscipline = '$academicdiscipline' AND flux_student_records.studentname = '$studentname'";
$retval = mysql_query($sql) or die(mysql_error());
将这些多个选定行插入另一个表的最佳方法是什么?谢谢。
答案 0 :(得分:1)
"选择进入"是要走的路。
SELECT into YourNewTable select project_proposals.id, project_proposals.title, project_proposals.description, project_proposals.academicname, flux_student_records.studentname, flux_student_records.id, flux_student_records.programme, flux_student_records.academicdiscipline FROM project_proposals JOIN flux_student_records ON project_proposals.academicdiscipline = flux_student_records.academicdiscipline
WHERE project_proposals.academicdiscipline = '$academicdiscipline' AND flux_student_records.studentname = '$studentname'";
希望你觉得这很有用。
答案 1 :(得分:0)
您可以循环播放帖子,然后使用该信息执行某些操作。 例如:
<?php
function readDataForwards($dbh) {
$sql = 'SELECT * FROM yourdb';
try {
$stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
$data = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n";
print $data;
#insert the data into another database
#you can also filter some duplicated items that you already
#have in the database
}
$stmt = null;
}
catch (PDOException $e) {
print $e->getMessage();
}
}
?>
这将输出:数据库中的行 但它也可以将行插入数据库。