MySQL从记录列表中搜索并替换记录
让我们首先创建一个表-
mysql> create table DemoTable -> ( -> ListOfName text -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Carol,Sam,John,David,Bob,Mike,Robert,John,Chris,James,Jace');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+------------------------------------------------------------+ | ListOfName | +------------------------------------------------------------+ | Carol,Sam,John,David,Bob,Mike,Robert,John,Chris,James,Jace | +------------------------------------------------------------+ 1 row in set (0.00 sec)
这是从记录列表中搜索和替换记录的查询-
mysql> update DemoTable -> set ListOfName= -> concat(substring_index(ListOfName,'John',2) ,'Adam', SUBSTRING_INDEX(ListOfName, 'John', -1)); Rows matched: 1 Changed: 1 Warnings: 0
让我们再次检查表记录-
mysql> select *from DemoTable;
这将产生以下输出-
+------------------------------------------------------------+ | ListOfName | +------------------------------------------------------------+ | Carol,Sam,John,David,Bob,Mike,Robert,Adam,Chris,James,Jace | +------------------------------------------------------------+ 1 row in set (0.00 sec)