mysql如何批量合并两个表的数据?

一、表结构一致

  • 允许插入重复数据
    insert into 表1 select * from 表2;

     

  • 不允许插入重复数据
    insert into 表1 select * from 表2 where NOT EXISTS (select * from 表1 where 字段1 = 表2.字段1);

     

二、表结构不一致

  需要指定字段名,并且需要一一对应

  • 允许插入重复数据
    insert into 表1(字段1,字段2,字段3...) select 字段1,字段2,字段3... from 表2; 

     

  • 不允许插入重复数据
    insert into 表1(字段1,字段2,字段3...) select 字段1,字段2,字段3... from 表2 where NOT EXISTS (select * from 表1 where 字段1 = 表2.字段1);

     

 

posted @ 2022-06-22 12:21  eliwang  阅读(645)  评论(0编辑  收藏  举报