问题描述
表1-ID,名称,地址,项目,日期,价格...
表2-ID,名称,项目
表2不能有重复的行(而表1可能有一些)。
通过使用“在不存在的地方插入”,插入其中的包含行本身会插入吗?
insert into table2 (id,name,item)
select id,item
from table1
where not exists
(select 1 from table2 where table2.id=table1.id and table2.name=table1.name and table2.item=table1.item)
解决方法
不。可以使用select distinct
处理此问题:
insert into table2 (id,name,item)
select distinct id,item
from table1
where not exists
(select 1 from table2 where table2.id=table1.id and table2.name=table1.name and table2.item=table1.item) ;