mysql:如何避免插入重复记录?

hec6srdp  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(355)

我做了如下插入声明:

Insert into table2 (host, ip, domain, statusnew) 
select hostname, ip, domain, "True" from table1 t1, table2 t2 
where t1.status = "Done"
and t2.statusnew not regexp "True"
limit 10
;

我已经补充了声明 t2.statusnew not regexp "True" 只是为了确保没有重复插入。但是,它正在添加重复的行。
如何确保没有重复条目?

bakd9h0s

bakd9h0s1#

您好您可以使用忽略关键字,以避免重复记录。比如:
在雇员(姓、名)值('rinku','gohel')中插入ignore;

jmp7cifd

jmp7cifd2#

INSERT INTO TABLE_2
  (id, name)
SELECT t1.id,
       t1.name
  FROM TABLE_1 t1
 WHERE NOT EXISTS(SELECT id FROM TABLE_2 t2 WHERE t2.id = t1.id)

相关问题