REPLACE ( string_expression , string_pattern , string_replacement ) 替换字符串,接受3个参数,分别是原字符串,被替代字符串,替代字符串。
SELECT username FROM user;
+-----------------------------+
| username |
+-----------------------------+
| peter |
| pedro |
| jerry |
+-----------------------------+
通过REPLACE
我们可以替换用户名中的pe
为we
。
SELECT REPLACE(username,'pe','we') FROM user;
+-----------------------------+
| REPLACE(username,'pe','we') |
+-----------------------------+
| weter |
| wedro |
| jerry |
+-----------------------------+
将user
表中的username
字段中的pe
替换为we
。(这里没有加where
条件,将会修改整张表中的数据)
UPDATE user set username=REPLACE(username, "pe", "we");
SELECT username FROM user;
+-----------------------------+
| username |
+-----------------------------+
| weter |
| wedro |
| jerry |
+-----------------------------+