sql语句之update笔记

sql 语句更新篇

基础篇

  • 基本赋值更新
1
update [table] set [field] = [value] where [where case];
  • 自增、自减等
1
2
3
4
5
6
update [table] set [field] = [field] + 1 where [where case];
update [table] set [field] = [field] - 1 where [where case];
update [table] set [field] = [field] * 1 where [where case];
update [table] set [field] = [field] / 1 where [where case];

update [table] set [field] = [field] + [an other field] 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