MySQL字符串URL中的最后一个索引?

要获取最后一个索引,请使用MySQL的SUBSTRING_INDEX()函数。语法如下-

SELECT yourColumnName1,...N,SUBSTRING_INDEX(yourColumnName,’yourDelimiter’,-1)as anyVariableName from yourTableName;

为了理解上述语法,让我们创建一个表。创建表的查询如下

mysql> create table LastIndexString
   -> (
   -> Id int,
   -> yourURL text
   -> );

使用INSERT命令在表中插入一些记录。查询如下-

mysql> insert into LastIndexString values(1,'https −//www.example.com/home.html');

mysql> insert into LastIndexString values(2,'https −//exampledemo.example.com/index.jsp');

mysql> insert into LastIndexString values(3,'https −//www.example.com/question/LastString');

使用SELECT语句显示表中的所有记录。查询如下-

mysql> select *from LastIndexString;

以下是显示URL字符串的输出-

+------+---------------------------------------------+
| Id   | yourURL                                     |
+------+---------------------------------------------+
| 1    | https −//www.example.com/home.html          |
| 2    | https −//exampledemo.example.com/index.jsp  |
| 3    | https −//www.example.com/question/LastString|
+------+---------------------------------------------+
3 rows in set (0.00 sec)

这是从URL字符串中获取最后一个索引字符串的查询-

mysql> select Id, substring_index(yourURL,'/',-1) as LastStringFromURL from LastIndexString;

以下是显示URL部分的输出-

+------+-------------------+
| Id   | LastStringFromURL |
+------+-------------------+
|    1 | home.html         |
|    2 | index.jsp         |
|    3 | LastString        |
+------+-------------------+
3 rows in set (0.00 sec)