php - Moving records to a new table updating original id accordingly -
i need create new table data table update original table id of newly inserted record new table. so:
new_table ---------------- id -- other data -- original_table ---------------- id new_table_id -- other data --
however, added records new_table grouped rid of duplicates. so, won't 1-to-1 insert. query needs update matching records, not copied record.
can in 1 query? i've tried doing separate update on original_table it's not working.
any suggestions?
you going doing 3 seperate queries see it.
$db = new pdo("..."); $stmt = $db->prepare("select * table"); $stmt->execute(); $results = $stmt->fetchall();just iterate o foreach ($results $result) { $stmt = "insert new_table (...) values (...)"; $stmt = $pdo->prepare($stmt); $data = $stmt->execute(); $insert_id = $pdo->lastinsertid(); // update first table $stmt = "update table set id=:last id=:id"; $stmt = $pdo->prepare($stmt); $data = $stmt->execute(array('last' => $insert_id, 'id' => $result['id'])); }
the above global example of workflow.
Comments
Post a Comment