How to insert records from table to another without duplicate

后端 未结 5 1660
小鲜肉
小鲜肉 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:31

    You can create a unique index (of one or more columns) and then use the MySQL replace command.

    CREATE UNIQUE INDEX unique_name2 ON t2 (name2);
    
    REPLACE INTO t2 (name2) values ($name1);
    ...
    

提交回复
热议问题