MySQL query 可以在列中用目录链接替换最后一个“/”之后的字符串吗?
为此,请使用 substring_index() 方法。我们首先创建一个表格:
mysql> create table DemoTable -> ( -> FolderName varchar(100), -> FolderLocation varchar(200) -> ); Query OK, 0 rows affected (1.03 sec)
使用 insert 命令在表格中插入一些记录:
mysql> insert into DemoTable values('CProgram','C:/AllPrograms/.....'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Images','E:/MyImage/home/garbage'); Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表格中的所有记录:
mysql> select *from DemoTable;
输出
+------------+-------------------------+ | FolderName | FolderLocation | +------------+-------------------------+ | CProgram | C:/AllPrograms/..... | | Images | E:/MyImage/home/garbage | +------------+-------------------------+ 2 rows in set (0.00 sec)
以下是用目录链接替换列中最后一个“/”之后的字符串的查询:
mysql> UPDATE DemoTable SET FolderLocation = CONCAT(LEFT(FolderLocation , CHAR_LENGTH(FolderLocation ) - CHAR_LENGTH(SUBSTRING_INDEX(FolderLocation , '/', -1))),FolderName); Query OK, 2 rows affected (0.16 sec) Rows matched: 2 Changed: 2 Warnings: 0
让我们再次查看表格记录:
mysql> select *from DemoTable;
输出
+------------+-------------------------+ | FolderName | FolderLocation | +------------+-------------------------+ | CProgram | C:/AllPrograms/CProgram | | Images | E:/MyImage/home/Images | +------------+-------------------------+ 2 rows in set (0.00 sec)
广告