“在不存在的地方插入”是否包括在同一插入处插入的记录

问题描述

表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) ;

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...