Update a column of text with MySQL REPLACE()
让我们首先创建一个表-
1 2 3 4 5 | mysql> create table DemoTable ( Code varchar(100) ); Query OK, 0 rows affected (0.50 sec) |
使用insert命令在表中插入一些记录-
1 2 3 4 5 6 | mysql> insert into DemoTable values('8565-9848-7474'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('9994-6464-8737'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('6574-9090-7643'); Query OK, 1 row affected (0.17 sec) |
使用select语句显示表中的所有记录-
1 | mysql> select *from DemoTable; |
这将产生以下输出-
1 2 3 4 5 6 7 8 | +----------------+ | Code | +----------------+ | 8565-9848-7474 | | 9994-6464-8737 | | 6574-9090-7643 | +----------------+ 3 rows in set (0.00 sec) |
以下是替换列值的查询-
1 2 3 4 | mysql> update DemoTable set Code=replace(Code,'9994-6464-8737','9994-9999-88888'); Query OK, 1 row affected (0.10 sec) Rows matched : 3 Changed : 1 Warnings : 0 |
让我们再次检查表记录-
1 | mysql> select *from DemoTable; |
这将产生以下输出-
1 2 3 4 5 6 7 8 | +-----------------+ | Code | +-----------------+ | 8565-9848-7474 | | 9994-9999-88888 | | 6574-9090-7643 | +-----------------+ 3 rows in set (0.00 sec) |