MySQL 如何从查询中获取文件扩展名
为了从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)
使用插入命令向表中插入一些记录。
查询如下:
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 教程
极客教程