简体   繁体   中英

How to update a table on one database from data of another table in another database in mysql

I am handling two online stores using different databases with exact same table names . Both are identical databases in structure. What I need to do is to connect these two databases.

products are adding separately to two stores . I need to add some products to store2 from store1 by selecting (like adding a button to go to store2 table).

When I click a product in store1 to goto store2 I like to create function to run in my store1 copying all the table values to insert into store2 database table . Is this possible? can you guide me to how to write mysql to implement thing like this.

(I am only asking for how to write the mysql part)

Thanks in Adavnce

As long as both databases are on the same server, you should be able to access tables form each database by fully qualifying the database name.

To copy rows (products) from store1 to store2 , the query would look something like this:

INSERT INTO store2.product_table
SELECT * FROM store1.product_table
WHERE store1.product_table.id = 1;

You can consider using federated mysql if you want to transparently copy data from one table in one server to another table in another server.

Another solution is to use an Extract Transform and Load (ETL) software.

You can also possibly have your php script connect to both databases and handle the following cases:

  • In db1 and not in db2
  • In db2 and not in db1
  • In both db1 and db2

In the last case, you might have to check if the two records are different and decide which one should overwrite the other.

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