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
CodePudding user response:
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;
CodePudding user response:
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.