如何通过 MySQL 查询获取文件的文件扩展名?
mysqlmysqli database更新于 2025/1/6 5:00:17
为了通过 SQL 查询获取文件的文件扩展名,可以使用 SUBSTRING_INDEX()。
语法如下
select substring_index(yourColumnName,'.',-1) as anyAliasName from yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下
mysql> create table getFileExtensionDemo -> ( -> File_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> File_Name text -> ); Query OK, 0 rows affected (0.53 sec)
使用 insert 命令在表中插入一些记录。
查询如下
mysql> insert into getFileExtensionDemo(File_Name) values('John.AllMySQLConcept.doc');
Query OK, 1 row affected (0.17 sec)
mysql> insert into getFileExtensionDemo(File_Name) values('Introductiontojava.txt');
Query OK, 1 row affected (0.17 sec)
mysql> insert into getFileExtensionDemo(File_Name) values('C and C++.AllDataStructureandAlgorithm.pdf');
Query OK, 1 row affected (0.14 sec)
mysql> insert into getFileExtensionDemo(File_Name) values('C.Users.Desktop.AllMySQLScript.sql');
Query OK, 1 row affected (0.39 sec)
使用 select 语句显示表中的所有记录。
查询如下
mysql> select *from getFileExtensionDemo;
以下是输出 −
+---------+--------------------------------------------+ | File_Id | File_Name | +---------+--------------------------------------------+ | 1 | John.AllMySQLConcept.doc | | 2 | Introductiontojava.txt | | 3 | C and C++.AllDataStructureandAlgorithm.pdf | | 4 | C.Users.Desktop.AllMySQLScript.sql | +---------+--------------------------------------------+ 4 rows in set (0.00 sec)
以下是查询结果,用于获取文件扩展名
mysql> select substring_index(File_Name,'.',-1) as AllFileExtension from getFileExtensionDemo;
以下是仅包含文件扩展名的输出
+------------------+ | AllFileExtension | +------------------+ | doc | | txt | | pdf | | sql | +------------------+ 4 rows in set (0.20 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

