需求:數據庫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字段中的空格