sql语句之update笔记
sql
语句更新篇
基础篇
- 基本赋值更新
1 | update [table] set [field] = [value] where [where case]; |
复制
- 自增、自减等
1 | update [table] set [field] = [field] + 1 where [where case]; |
复制
- 更新多个字段
在
set
后面用逗号隔开便可
1 | update [table] set [field] = [field] + 1, [field2] = 2 where [where case]; |
复制
- 更新记录数量限制等
和查询一样,更新也是可以接收
order by
,limit
的;
1 | update [table] set [field] = [field] + 1, [field2] = 2 where [where case] order by [field] limit [limit] offset [offset]; |
复制
- 替换更新
写入了记录发现要批量更新,但是只能更新字段的一部分,这时候就可以用替换了
以下是清理图片后缀的语句.
1 | update [table] set `image` = REPLACE(`image`, '.png', '')where `image` LIKE '%.png%'; |
复制
- 字符串合并
这个很少会用到,一般是在存储过程里面使用
以下是我当初维护数据时,为了清除重复数据做出的标记改动,以便我新增唯一索引
1 | update [table] set union_data = concat(`union_data`,'-',`id`) where [where case]; |
复制
To be continue