mysql - MYSQL:如何避免插入重复记录?
问题描述
我正在做如下插入语句:
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"
只是为了确保没有重复插入。但是,它正在添加重复的行。
如何确保没有重复条目?
解决方案
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)
推荐阅读
- javascript - Ajax 获取外部 JS 而不加载它
- javascript - 反应引导模式上下文
- qt - Qt:在使用 QSignalMapper 连接动作时,可以使用 QStringList 作为函数参数吗?
- .htaccess - XAMPP mod_rewrite 对象未找到错误
- scala - 我们不能在 map 函数中使用 sparkContext 吗?
- python - Python Bbcode 到 HTML 翻译
- python - Spyder 无法加载
- php - 如何计算 Google 表格中的行数?
- selenium - 如何使用 Selenium WebDriver 捕获网络流量并查找谷歌分析调用?
- python - Python-Flask 异常:意外的数据类型
,