sql - “在不存在的地方插入”是否包括在同一插入处插入的记录
问题描述
表 1 - id、姓名、地址、项目、日期、价格、...
表 2 - id、name、item
表 2 不能有重复的行(而表 1 可能有一些)。
通过使用“在不存在的地方插入”,它包含的插入该插入的行是否会插入自身?
insert into table2 (id,name,item)
select id, name, item
from table1
where not exists
(select 1 from table2 where table2.id=table1.id and table2.name=table1.name and table2.item=table1.item)
解决方案
不,这可以使用以下方法处理select distinct
:
insert into table2 (id, name, item)
select distinct id, name, item
from table1
where not exists
(select 1 from table2 where table2.id=table1.id and table2.name=table1.name and table2.item=table1.item) ;
推荐阅读
- matlab - 如何在 MATLAB 中计算方程中符号变量的数量?
- mongodb - 可分页请求如何工作?
- mysql - 无法在谷歌应用脚本中执行包含中文字符的mysql查询
- python - 从 FileDialog Python\Qt 获取文本
- r - R - 时间序列图仅显示在控制台中,而不是 Web 应用程序
- encryption - 将 db 文件加密为 crypt12 格式
- firebase - Firebase RemoteConfig 和受众条件
- android - 从不同类中的小吃店开始意图导致错误
- c# - 成功 selenium 登录测试到网站后的错误消息
- python - python参数有或没有值,指定与否