当前位置: 首页 > 工具软件 > sqlupdate > 使用案例 >

sql update语句

谢选
2023-12-01

1)常规的update语法:update 表名 set 字段名 = 值 where 条件

2)替换原有字段中的某个值:
需求:mit_ipran_config_items表有字段cmds,值为'...||undo dcn||...'   ,需要在||undo dcn前面加上||dcn

可以使用replace方法:replace(字段名,原有值,修改后的值)

update语法:update 表名 set 字段名 = replace(字段名,原有值,修改后的值) where id = xxx

这里是:update mit_ipran_config_items set cmds = replace(cmds,'||undo dcn','||dcn||undo dcn') where id = xxx

3)将原有字段值复制到指定字段中

需求:现有表mit_ipran_config_items,需要将id=1的字段cmds的值复制到id=2的cmds中

需要使用join,update语法:update 表名1 join 表名2 set 表名1.字段名 = 表名2.字段名 where 表名1.id = xxx

这里是: update mit_ipran_config_items as t1 join (select cmds from mit_ipran_config_items where id =1) as t2 set t1.cmds= t2.cmds where t1.id =2

4)使用update更新多个字段,set后面的字段逗号分隔

update 表名 set 字段名1 = 值, 字段名2 = 值 where 条件

5)in 集合:where 字段名 in (字段值1,字段值2,...)

如,修改多条记录的device_kind字段:UPDATE mit_ipran_config_items set device_kind = '950C' WHERE id in (8041,8042);

 

 类似资料: