insert select 过滤掉重复数据

INSERT INTO SELECT 语句从一个表复制数据,然后把数据插入到一个已存在的表中。

table1和table2表结构一样时:

INSERT INTO table2
SELECT * FROM table1;

table1和table2表结构不一样时:

INSERT INTO table2(field1,field2,field3)
SELECT field1,field2,field3 FROM table1;

NOT EXISTS 过滤掉重复的数据:

INSERT INTO
	table1(field1,field2,field3)
SELECT field1,field2,field3 FROM table2 a
WHERE NOT EXISTS (SELECT * FROM table1 b where a.field1=b.field1)

你可能感兴趣的:(SQL,insert,select,not,exists)