Question

Let's say for instance:

  • I have two tables: old_data and new_data.

  • Both old_data and new_data have one column called this_is_col.

  • Both old_data and new_data have various (hundreds) of rows of dates (2010-02-06, 2010-01-09, 2007-06-02, etc.). Both tables don't necessarily have the same dates, but they both have the same format.

  • The fields of both tables are various integers.


My task:

  • Copy the fields from old_data to new_data.

  • If a date exists in both tables, the field in new_data will be replaced.

  • If the date doesn't exist in new_data, then the correct row will be added and the field will be copied over.


Here is how far I've gotten:

Create a temporary column:

ALTER TABLE `new_data` ADD `tempColumn` TEXT NULL;

Copy over data from old_data:

INSERT INTO `new_data` (`tempColumn`) SELECT `this_is_col` FROM `old_data`;

Combine temporary column and new_data . this_is_col. (I haven't really figured this step out since I haven't gotten this far).

MERGE? `tempColumn` `this_is_col`;

Delete temporary table

ALTER TABLE `new_data` DROP `tempColumn`;

Upon performing the second action (transferring the data over to the temporary column) I get this error:

#1062 - Duplicate entry '0000-00-00' for key 1

And now I'm stuck. Any help would be appreciated. I'm using MySQL and phpMyAdmin to test the SQL commands.

Was it helpful?

Solution

Assuming your dates are indexed as unique keys:

INSERT INTO newtable
SELECT *
FROM oldtable
ON DUPLICATE KEY column1=oldcolumn1, ...

OTHER TIPS

you want INSERT ... ON DUPLICATE KEY UPDATE. your solution already satisfies steps 1 and 3 of your task, ON DUPLICATE KEY UPDATE will take care of step 2.

If you'd rather delete the row first, instead of updating: REPLACE

It'd be just one line too, so: REPLACE data SELECT, you wouldn't have to do the weirdness with adding a text column.

How about just doing an UPDATE and INSERT?

UPDATE new_data SET col=col 
FROM new_data a join old_data b on a.this_is_col = b.this_is_col

Then

INSERT INTO new_data (cols) SELECT cols 
FROM old_data WHERE this_is_col NOT IN (SELECT this_is_col FROM new_data)

Unless I misunderstood...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top