我有2个表equip_copy(copyID, equipment_id)并将其插入表

mre_copy (mreID,copyID,equipment_id)


我已经尝试过此Select查询,但不会移动。请任何人可以帮助我吗?

$display = $con->query("SELECT copyID,equipmentID
                         FROM equip_copy
                         WHERE equipmentID= :eid
                         ORDER BY copyID DESC
                         LIMIT :elimit");

$display->execute(array("eid" => $id, "elimit"=>$request));

foreach($display as $row){

        $newCID = $row['copyID'];
        $newEID = $row['equipmentID'];

        $sql_table = "INSERT INTO mre_copy(mreID,equipmentID,copyID) values(?,?,?)";
        $stmt = $con->prepare($sql_table);
        $stmt->execute(array($mreID,$newEID,$newCID));
}


php - PHP的PDO选择多行,并插入到其他表与限制-LMLPHP

最佳答案

使用insert . . . select。我想这就是你想做的

INSERT INTO mre_copy (mreID, equipmentID, copyID)
    SELECT :mreID, copyID, equipmentID
    FROM equip_copy
    WHERE equipmentID = :eid
    ORDER BY copyID DESC
    LIMIT :elimit;


我不确定mreID的来源。如果它是自动递增的,则将其完全放在INSERT之外。

09-07 15:50