I work in MySQL The task is as follows:

There is a table 1 in which there are goods with the field "article". In table 2 there is also a field

It is necessary to add table 2 to table 1, but so that the rows with the same part numbers are not added.

I.e:

Table 1 id Name Article

  1. Apple 111555
  2. Pear 111222
  3. Lemon 111888

Table 2

id Name Article

  1. Apple 222222
  2. Grushaz 111222
  3. Lemon377777

Total

id name Article

  1. Apple 111555
  2. Pear 111222
  3. Lemon 111888
  4. Apple 222222
  5. Lemon377777
  • Grushaz? ........... - Jean-Claude
  • one
    @ Han Gen. And where is your decision? What have you done? - Vladimir Glinskikh

3 answers 3

INSERT INTO table1 ( `name`, `art` ) SELECT t2.`name`, t2.`art` FROM table2 t2 WHERE NOT EXISTS (SELECT 1 FROM table1 t1 WHERE t1.`art` = t2.`art`) 
  • Thanks, works - Khan Gen.
  • @Khan Byty If my answer helped you, mark it as correct - Anton Shchyrov

Add to the first table a unique index on the field артикул

 CREATE UNIQUE INDEX idx_articul ON table1 (articul) 

then just insert the data

 INSERT IGNORE INTO table1 (name, articul) SELECT name, articul FROM table2 

Duplicates themselves will not be recorded.

    You can somehow

     SELECT name, articul FROM table_1 WHERE articul NOT IN ( SELECT DISTINCT articul FROM table_2 ); UNION SELECT name, articul FROM table_2 WHERE email NOT IN ( SELECT DISTINCT articul FROM table_1 );