要对 MySQL 数据库内的资料替换字串,可以借助 PHP 的 str_replace(),但这实在太麻烦了,其实MySQL 已内建了这个功能,以下语句可以将 [table] 资料表的 [field] 字段作 replace 动作,[str_search] 是搜索字串;[str_replace] 是替换字串。
update [table] set [field]=replace([field],’[str_search]‘,’[str_replace]‘);
如果不想将整个资料表的字段资料更换,可以在最后用 where 做条件选择,例如:
update [table] set [field]=replace([field],’[str_search]‘,’[str_replace]‘) where zip=’852′;
以上语句跟第一句的分别是,第二句只会对字段 “zip” 为 “852″ 的纪录作更改。
谢谢 🙂 你的文章帮了我好多忙 🙂