insert select 去重
时间: 2023-08-26 22:18:37 浏览: 96
mongodb中使用distinct去重的简单方法
可以使用NOT EXISTS来在INSERT SELECT语句中进行去重操作。通过在SELECT子句中使用NOT EXISTS子查询来判断要插入的数据是否已经存在于目标表中。具体操作如下:
INSERT INTO table1(field1, field2, field3)
SELECT field1, field2, field3
FROM table2 a
WHERE NOT EXISTS (
SELECT *
FROM table1 b
WHERE a.field1 = b.field1
)
这段代码中,我们将从table2表中选择field1、field2和field3字段的值,并将它们插入到table1表中。在WHERE子句中,我们使用NOT EXISTS子查询来判断在目标表table1中是否存在与当前正在插入的数据具有相同field1值的记录。如果不存在,则插入该记录。
这样可以确保插入到table1表中的数据不会与已存在的数据重复。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [MySql 批量插入,解决重复值问题](https://blog.csdn.net/qq_39360632/article/details/119816741)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [insert select 过滤掉重复数据](https://blog.csdn.net/farxix/article/details/102949923)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [Oracle 数据去重问题 Insert into select Distinct](https://blog.csdn.net/qq_32692315/article/details/87713144)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文