如何在 MySQL 中查找并替换带文件路径的列?


为此,使用 MySQL REPLACE()。我们首先创建一个表格−

mysql> create table DemoTable
(
   FolderLocation text
);
Query OK, 0 rows affected (0.80 sec)

使用 insert 命令在该表格中插入一些记录 -

mysql> insert into DemoTable values('C/ProgramFiles/AllMySQLProgram');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('C/ProgramFiles/JavaChatApplication');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('C/ProgramFiles/Main/Image.png');
Query OK, 1 row affected (0.18 sec)

使用 select 语句显示表格中的所有记录 -

mysql> select *from DemoTable;

这将生成以下输出 -

+------------------------------------+
| FolderLocation                     |
+------------------------------------+
| C/ProgramFiles/AllMySQLProgram     |
| C/ProgramFiles/JavaChatApplication |
| C/ProgramFiles/Main/Image.png      |
+------------------------------------+
3 rows in set (0.00 sec)

以下是执行 MySQL 查找并替换的查询 -

mysql> update DemoTable
   set FolderLocation=replace(FolderLocation,'C/ProgramFiles','E/MyFolder/Details');
Query OK, 3 rows affected (0.14 sec)
Rows matched: 3 Changed : 3 Warnings : 0

让我们再次检查表格记录 -

mysql> select *from DemoTable;

这将生成以下输出 -

+----------------------------------------+
| FolderLocation                         |
+----------------------------------------+
| E/MyFolder/Details/AllMySQLProgram     |
| E/MyFolder/Details/JavaChatApplication |
| E/MyFolder/Details/Main/Image.png      |
+----------------------------------------+
3 rows in set (0.00 sec)

更新于: 10-Oct-2019

371 次浏览量

开启你的 职业生涯

完成课程即可获得认证

立即开始
广告