简体   繁体   中英

Sync two tables from two different databases with different structure MySql

I have two php applications that deal with same products, one is shopping and the other is point of sale but each one uses it's own db.

Since they are both dealing with same products I want to shorten the time used in entering products data by syncing products tables in the 2 dbs so when I add a new product in the shop it shows in the POS.

I searched a lot but all results were talking about identical tables structure.

Unfortunately in my case the structure is different.

Shop products table is like this:

id    name    current_stock  category_id   brand_id

POS products table is like this:

id   name    code    category_id   brand_id

and another issue that the code column in the pos db should contain a unique number I use this command to generat it:

`code` =  FLOOR(1000 + RAND() * 89999999)

I need it to be generated in the sync process.

I saw this and many other posts how to copy only distinct values from one table to another in Mysql? and it's about identical tables

Edit: At first I'll add all products but after that I'll need to add newer products only. This what I mean by syncing

I am assuming that both tables are inside the same mysql server and your mysql user account have correct privileges to access both.

INSERT INTO database_pos.pos_product 
        SELECT id, name, 
               FLOOR(1000 + RAND() * 89999999) as code,
                 category_id, brand_id FROM database_shop.shop_product;

I do not recommend FLOOR(1000 + RAND() * 89999999) to generate unique key.

This statement will query all the data from shop product table and then insert it into POS product table. Change the table and database names according to your schema.

But as @stickybit suggests. I would also recommend you to move both tables into same database if possible.

Also, you can implement a trigger to automate this.

EDIT: To add the records which are not already in the table. You have to select only those records from the shop products table.

Using subquery to get set difference,

INSERT INTO database_pos.pos_product 
    SELECT id, name, 
           FLOOR(1000 + RAND() * 89999999) as code,
           category_id, brand_id FROM database_shop.shop_product sp
        WHERE sp.id NOT IN (SELECT id FROM database_pos.pos_product);

Using outer join to get set difference,

INSERT INTO database_pos.pos_product 
    SELECT sp.id, sp.name, 
           FLOOR(1000 + RAND() * 89999999) as code,
           sp.category_id, sp.brand_id FROM database_shop.shop_product sp
    LEFT JOIN database_pos.pos_product pp ON pp.id = sp.id
           WHERE pp.id IS NULL;

Subquery vs Join

Mysql triggers tutorial

Mysql triggers official doc

Thanks !

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