在 MySQL 中从路径中提取文件名?
mysqlmysqli database
要从 MySQL 路径中提取文件名,可以使用 SUBSTRING_INDEX()。语法如下 −
SELECT SUBSTRING_INDEX(ypurColumnName, '\', -1) as anyAliasName FROM yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下 −
mysql> create table ExtractFileNameDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> AllProgrammingFilePath varchar(100) -> ); Query OK, 0 rows affected (0.50 sec)
现在,您可以使用 insert 命令在表中插入一些记录。查询如下 −
mysql> insert into ExtractFileNameDemo(AllProgrammingFilePath) values('C:\Users\John\AddTwoNumberProgram.java'); Query OK, 1 row affected (0.13 sec) mysql> insert into ExtractFileNameDemo(AllProgrammingFilePath) values('E:\CProgram\MasterMindGame.c'); Query OK, 1 row affected (0.23 sec) mysql> insert into ExtractFileNameDemo(AllProgrammingFilePath) values('F:\WebApplication\WebApp.php'); Query OK, 1 row affected (0.19 sec) mysql> insert into ExtractFileNameDemo(AllProgrammingFilePath) values('C:\Users\John\Desktop\AllMySQLScript.sql'); Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表中的所有记录。查询如下 −
mysql> select *from ExtractFileNameDemo;
以下是输出 −
+----+------------------------------------------+ | Id | AllProgrammingFilePath | +----+------------------------------------------+ | 1 | C:\Users\John\AddTwoNumberProgram.java | | 2 | E:\CProgram\MasterMindGame.c | | 3 | F:\WebApplication\WebApp.php | | 4 | C:\Users\John\Desktop\AllMySQLScript.sql | +----+------------------------------------------+ 4 rows in set (0.00 sec)
这是从 MySQL 中的路径中提取文件名的查询 −
mysql> select SUBSTRING_INDEX(AllProgrammingFilePath, '\', -1) as AllFileName from ExtractFileNameDemo;
以下是输出 −
+--------------------------+ | AllFileName | +--------------------------+ | AddTwoNumberProgram.java | | MasterMindGame.c | | WebApp.php | | AllMySQLScript.sql | +--------------------------+ 4 rows in set (0.00 sec)