How to insert records from table to another without duplicate

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

    insert into test.t2(name2) 
    select distinct name1 from test.t1 where name1 NOT IN(select name2 from test.t2);
    

    OR

    insert into test.t2(name2) 
    select distinct name1 from test.t1 t1 where NOT EXISTS(select name2 from test.t2 t2 where t1.name1=t2.name2);
    

提交回复
热议问题