Question

I've two same tables(same table columns and primary key) in two different databases. I want to add 2nd table data to the first table that not exist in the first table (according to the primary key). what is the best method to do that?

I can export 2nd table data as csv, php array or sql file.

Thanks

No correct solution

OTHER TIPS

There are lots of ways to do this.

The simplest is probably this one:

INSERT IGNORE
  INTO table_1 
SELECT *
  FROM table_2
     ;

which allows those rows in table_1 to supersede those in table_2 that have a matching primary key, while still inserting rows with new primary keys.

Alternatively, you can use a subquery to find out the rows that are not shared by both tables and insert them. If you've got a lot of records, you may want to consider using a temporary table to speed up the process.

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