关于一张表数据插入到另一张表保持数据唯一,不重复

insert into a2 (c1,c2)
select c1,c2 from a where Deleted=1


能提供一些思路吗..或者mysql本身能解决吗

假如表中已存在该数据则不能插入该数据,不存在就插入
网友回复:先在a2表中查找你要插入的数据,假如能找到就说明有重复数据,不要插入。
网友回复:sql 语句能解决吗 例如distinct , if exists
网友回复:一條語句搞定,不過不知道MSSQL的寫法與MySQL的是否一致
SQL code
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
insert   into   a2   (c1,c2)
select   c1,c2   from   a
left join a2 on a.c1=a2.c1 and a.c2=a2.c2
where   Deleted=1 and (a2.c1 is null and a2.c2 is null)
 
如果是一个主键我可以用   not   in   来判断,     
  但是两个键如何写  
  我试用exists来,   但写不好,   它只判断存不存在,   到底怎么写大家教我
网友回答:
网友:tj_dns

insert   b  
  select   *   from   a   where   not   exists   (select   *   from   b   where   k1   =   a.k1   and   k2   =   a.k2)

网友:pengdali

insert   b   select   *   from   a   where   not   exists   (select   1   from   b   where   k1   =   a.k1   and   k2   =   a.k2)

 


原文地址:https://www.cnblogs.com/chengulv/p/1057518.html