您需要使用MySQL的substring_index()函数来选择文本。
语法如下
SELECT substring_index(yourColumnName,'/',-1) AS anyAliasName FROM yourTableName;
为了理解上述概念,让我们创建一个表。创建表的查询如下
mysql> create table selectTextAfterLastSlashDemo - > ( - > UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > UserPathDirectory varchar(200) - > );
使用insert命令在表中插入一些记录。
查询如下
mysql> insert into selectTextAfterLastSlashDemo(UserPathDirectory) values('C:/MyFolder1/MyEntityFramework'); mysql> insert into selectTextAfterLastSlashDemo(UserPathDirectory) values('D:/MySpringFrameworkDemo'); mysql> insert into selectTextAfterLastSlashDemo(UserPathDirectory) values('E:/Java/MyRootFolder/Source/AllHibernateDemo'); mysql> insert into selectTextAfterLastSlashDemo(UserPathDirectory) values('C:/Program Files/MySQL/Server 8.0'); mysql> insert into selectTextAfterLastSlashDemo(UserPathDirectory) values('C:/John/Folder1/Folder2');
使用select语句显示表中的所有记录。
查询如下
mysql> select *from selectTextAfterLastSlashDemo;
以下是各列中的输出显示路径
+--------+----------------------------------------------+ | UserId | UserPathDirectory | +--------+----------------------------------------------+ | 1 | C:/MyFolder1/MyEntityFramework | | 2 | D:/MySpringFrameworkDemo | | 3 | E:/Java/MyRootFolder/Source/AllHibernateDemo | | 4 | C:/Program Files/MySQL/Server 8.0 | | 5 | C:/John/Folder1/Folder2 | +--------+----------------------------------------------+ 5 rows in set (0.00 sec)
这是在最后一个斜杠后获取文本的查询
mysql> select substring_index(UserPathDirectory,'/',-1) as TextAfterLastSlash from selectTextAfterLastSlashDemo;
以下是显示最后一个斜杠后的文本的输出
+-----------------------+ | TextAfterLastSlash | +-----------------------+ | MyEntityFramework | | MySpringFrameworkDemo | | AllHibernateDemo | | Server 8.0 | | Folder2 | +-----------------------+ 5 rows in set (0.00 sec)