需求:数据库info表中name字段,值为Beijingcar、Beijingpeople、Beijinghouse等;将该字段以Beijing开头更新成BJ开头。
解决:可以使用MySQL内置的REPLACE函数,具体用法如下:
UPDATE 表名 SET 字段名 = REPLACE(object, \'search\', \'replace\'); update info set name = REPLACE(name,\'Beijing\',\'BJ\') where name like \'Beijing%\'; 扩展:update `news` set `content`=replace(`content`,\' \',\'\');//清除news表中content字段中的空格