mysql的REPLACE说明:
REPLACE(str,from_str,to_str)
在字符串 str 中所有出现的字符串 from_str 均被 to_str替换,然后返回这个字符串
例如:
把表table中的name字段中的aa替换为bb
update table set name=replace(name,'aa','bb')
一个表字段,原来是纯文本编辑格式,后修改功能为支持HTML
原来显示:
111
222
修改后,显示为:111 222
为了使其正确显示,修改数据库数据为HTML格式;
111
222
即:
111
222
处理方法:
复制代码 代码示例:
update `table_a` set `field`=Replace(`field`,"\r\n","
");
//把"\r\n"替换成"
"
//=>111
222
update `table_a` set `field`=CONCAT(`field`,"
");
//在字段后面补上"
"
//=>111
222
update `table_a` set `field`=CONCAT("
",`field`);
//在字段前面增加"
"
//=>
111
222
update `table_a` set `field`=Replace(`field`,"
//去除字段最后的"
//=>
111
222
即:
111
222
如此页面就可以正常显示了。
注:纯文本,标准换行符为:\r\n,不标准的,有可能显示为\r或\n,如果遇到不标准的数据,可进行如下处理:
update `table_a` set `field`=Replace(`field`,"\r\n","
");修改为:
复制代码 代码示例:
update `table_a` set `field`=Replace(`field`,"\r\n","\r");
update `table_a` set `field`=Replace(`field`,"\n","\r");
update `table_a` set `field`=Replace(`field`,"\r","
");
即统一换行符格式后,再处理数据即可。
您可能感兴趣的文章:
mysql replace的用法(替换指定字段字符串)
mysql使用replace函数进行字符串替换