sql - 如何在不重复的情况下更新
问题描述
我有一张桌子
ID | 姓名 | 类型 |
---|---|---|
1 | 一种 | 15 |
2 | 一种 | 14 |
3 | C | 14 |
4 | C | 15 |
5 | d | 15 |
6 | F | 14 |
7 | G | 15 |
我想将 type = 15 的所有位置更新为 14。但是已经有一些同名的类型 14 我怎样才能只更新不同的
解决方案
如果您想避免会产生重复的更新行,您可以not exists
使用update
:
update t
set type = 14
where type = 15 and
not exists (select 1
from t t2
where t2.name = t.name and t2.type = 14
);
推荐阅读
- excel - 复制列中重复次数少于 100 次的数据 - Excel-VBA
- javascript - node.js 中的空循环行为
- amazon-ec2 - Anypointstudio aws EC2
- java - 按名称排序,然后添加每个同名对象的数量?
- reactjs - TypeScript + React 获取不同类型的数组
- java - 在没有 if else 的情况下动态选择依赖项
- javascript - 在三个js中突出显示鼠标指针最近的边缘
- r - 在 R 中,如何在点 sf 对象上运行 st_convex_hull 函数?
- ruby-on-rails - 使用辅助方法减少调用 ActiveAdmin.register 的行数
- php - 如何将对象数组转换为键值对数组?