SQL query - table cloning

I have three tables, I don’t know if I need to use them all, but basically I need to compare table A and table C, if there is an identifier that is in table A that is not in table C, then in table C the identifier should be added along with the value in another cell in this row. The following is an example:

Original table A:

------------------------- Sku | Status ------------------------- ABC | Enabled DEF | Enabled GHI | Enabled JKL | Disabled MNO | Enabled 

Original table C:

 ------------------------- Sku | Status ------------------------- ABC | Enabled DEF | Enabled GHI | Enabled 

After querying table C:

 ------------------------- Sku | Status ------------------------- ABC | Enabled DEF | Enabled GHI | Enabled JKL | Disabled MNO | Disabled 
+4
source share
3 answers

This will work in MySQL:

 INSERT INTO b SELECT * FROM a WHERE NOT EXISTS (SELECT * FROM b WHERE b.sku = a.sku); 
+1
source

Good question!! Try something similar to:

 INSERT INTO TableC (SELECT * FROM TableA WHERE TableA.id NOT IN (SELECT id FROM TableC) ) 
+1
source

not sure if mysql supports this syntax, but this should also work:

 insert into c (sku, status) (select sku, status from a where not exists (select null from c where c.sku = a.sku)) ; 
0
source

Source: https://habr.com/ru/post/1388269/


All Articles