简体   繁体   中英

Moving records to a new table updating original id accordingly

I need to create a new table with certain data from another table but update the original table with the ID of the newly inserted record from the new table. Like so:

NEW_TABLE
----------------
id
-- other data --


ORIGINAL_TABLE
----------------
id
new_table_id
-- other data --

However, the added records to new_table will be grouped to get rid of duplicates. So, it won't be a 1-to-1 insert. The query needs to update matching records, not just the copied record.

Can I do this in one query? I've tried doing a separate UPDATE on original_table but it's not working.

Any suggestions?

You can use temporary tables or create a view for NEW_TABLE .

Temporary Tables

You can use the TEMPORARY keyword when creating a table. A TEMPORARY table is visible only to the current session, and is dropped automatically when the session is closed. This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.) To create temporary tables, you must have the CREATE TEMPORARY TABLES privilege.

--Temporary Table
create temporary table NEW_TABLE as (select * from ORIGINAL_TABLE group by id);

Views

Views (including updatable views) are available in MySQL Server 5.0. Views are stored queries that when invoked produce a result set. A view acts as a virtual table. Views are available in binary releases from 5.0.1 and up.

--View
create view NEW_TABLE as select * from ORIGINAL_TABLE group by id;

The view will always be updated with the values in ORIGINAL_TABLE and you will not have to worry about having duplicate information in your database.

If you do not want to use the view, I believe you can only perform an insert on one table at a time unless you have some sort of view that would allow you to do both, but you probably want to do it as two steps in a transaction

First you will have to tell the database that you want to start a transaction . Then you will perform your operations and check to see if they were successful. You can get the id of last inserted row (this assumes you have an auto_increment field) to use in the second statement. If both statement seem to work fine, you can commit the changes, or if not, rollback the changes.

Example:

//Assume it will be okay
$success = true;

//Start the transaction (assuming you have a database handle)
$dbh->beginTransaction();

//First Query
$stmt = "Insert into ....";
$sth = $dbh->prepare($stmt);
//See if it works
if (!$sth->execute())
    $success = false;

$last_id = $dbh->lastInsertId();

//Second Query
$stmt = "Insert into .... (:ID ....)";
$sth = $dbh->prepare($stmt);
$sth->bindValue(":ID", $last_id);
//See if it works
if (!$sth->execute())
    $success = false;

//If all is good, commit, otherwise, rollback
if ($success)
    $dbh->commit();
else
    $dbh->rollBack();

You are going to be doing 3 seperate queries as I see it.

$db = new PDO("...");
$stmt = $db->prepare("SELECT * FROM table");
$stmt->execute();
$results = $stmt->fetchAll();just iterate o

foreach ($results as $result) {
    $stmt = "INSERT INTO new_table (...) VALUES (...)";
    $stmt = $pdo->prepare($stmt);
    $data = $stmt->execute();

    $insert_id = $pdo->lastInsertId();

    // Update first table
    $stmt = "UPDATE table SET id=:last WHERE id=:id";
    $stmt = $pdo->prepare($stmt);
    $data = $stmt->execute(array('last' => $insert_id, 'id' => $result['id']));
}

The above is a global example of your workflow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM