如何将记录从表中移动到具有所选字段的其他表?

时间:2016-02-02 02:12:53

标签: php mysql

我需要将记录移动到其他表但我想要一个特定的行或字段,就像我想在unvoted_logs中移动学生(表)记录但是它们是不同的字段所以我必须具体插入,我有一个代码但它没有用,请我需要帮助

Data structure

$stud ="INSERT INTO unvoted_logs(idno,syearid)
                            SELECT idno,syearid FROM SELECT st.* FROM student st LEFT  JOIN vote_logs sv ON st.idno = sv.idno AND st.syearid = sv.syearid
                            WHERE sv.idno IS NULL AND user_type='3'"; 
        $qa = $db->prepare($stud);

1 个答案:

答案 0 :(得分:0)

嗨,这是我所做的粗略伪代码。通过简短的解释,我将您提供的代码分为三个步骤。

首先,SQL从内部选择表创建一个视图。

其次,SQL从视图表中选择一条记录

第三种是通过单独的查询插入SQL。

希望它有所帮助。感谢。



$sql1 = "Create OR Replace View [student joins votes] AS
	SELECT * FROM student st LEFT JOIN vote_logs sv 
			ON st.idno = sv.idno AND st.syearid = sv.syearid";
$qa = $db->prepare($sql1);
$qa->execute();


$sql2= "Select idno,syearid FROM [student joins votes] where WHERE idno IS NULL AND user_type='3'";
$qa = $db->query($sql2);

//only has one row in the database
if ($qa->num_rows == 1) {
    // output data of each row
    while($row = $qa->fetch_assoc()) {
        $idno = $row["idno"];
		$syearid = $row["syearid"];
		break;
	}
} 

if($idno!=null && $syearid!=null){
	$qa = $db->prepare("INSERT INTO unvoted_logs (idno, syearid) 
		VALUES (:idno, :syearid)");
		$qa->bindParam(':idno', $idno);
		$qa->bindParam(':syearid', $syearid);
		$qa->execute();
}