向表 tabel #1插入新数据
怎么使 tabel#2 其中一个字段ssa与 表 tabel #1 字段ssa的值不重复
向表 tabel #1插入新数据
怎么使 tabel#2 其中一个字段ssa与 表 tabel #1 字段ssa的值不重复
描述不是很清楚,是将 table#2的数据插入到table#1里,还是其它地方的数据插入到table#2里?
如果是table#2的数据插入到table#1里的话
insert into table#1(ssa,ssb,ssc,ssd,sse)
select ssa,ssb,ssc,ssd,sse
from table#2 t1
where not exists (select 1 from table#1 t2 where t1.ssa=t2.ssa)
如果是mysql的话追加一个group by ssa 可以去掉table#2里重复的数据(重复的保留第一条)
sqlserver的话追加
and t1.id in (select min(id)
from table#2
group by ssa)
其中id 是主键
sqlserver的方式其它数据库应该也都能起效
如果要去重的话,最好还是字段加上唯一索引,不然根据墨菲定律,一定会出现重复数据