How to insert records from table to another without duplicate

后端 未结 5 1661
小鲜肉
小鲜肉 2021-01-21 17:01

I have two tables t1 and t2. t1 has duplicated values. I need to insert all records from t1 to t2, but I don\'t want duplicates to occur in t2. I tried the following command whi

5条回答
  •  春和景丽
    2021-01-21 17:08

    You've go two options here, one involves not duplicating the data on your insert, the second being to ignore duplicates when inserting.

    To de-duplicate from the SELECT call you'd use `DISTINCT:

    INSERT INTO test.t2 (name2) SELECT name1 FROM test.t1 WHERE name1 NOT IN (SELECT name2 FROM test.t2)
    

    You can also add a UNIQUE index to avoid this problem in the first place:

    ALTER TABLE t2 ADD UNIQUE INDEX index_name2 (name2)
    

    Note that you will get an error if there is already duplicate data in t2, so you may have to clean it up beforehand.

    Then you can add data with the IGNORE option:

    INSERT IGNORE INTO test.t2 (name2) SELECT name1 FROM TEST.t1
    

    The unique index approach will guarantee uniqueness.

提交回复
热议问题