在 mysql 中我们可以通过 replace 函数来替换指定字段的部分内容,具体用法如下:
1.replace into
replace into test (id,name) values('1','a'),('2','b')
此语句的作用是更新表 table 中主键 id 为 1 和 2 的两条记录,将 name 替换成 a 和 b。
如果主键 id 为 1 和 2 的记录不存在就执行新增操作,相当于
insert into table (id,name) values('1','a'),('2','b')
如果主键 id 为 1 和 2 的记录存在且值相同则不会更新任何数据。
2.replace(object,search,replace)
把 object 中出现 search 的全部替换为 replace
select replace('www.02405.com','www','txt')
输出:txt.02405.com
3.update 与 replace 组合使用更新指定字段中的指定内容
update 表名 set 字段 = replace(字段,search,replace)
示例:
update test set name = replace(name,'b','B')
上述语句会把 test 表 name 字段中所有的小写字母 b 替换成大写字母 B
本文为原创文章,版权归主机之家测评所有,欢迎分享本文,转载请保留出处!