URL 中的 MySQL 字符串最后索引?
要获取最后索引,请使用 MySQL 中的 SUBSTRING_INDEX() 函数。语法如下 −
SELECT yourColumnName1,...N,SUBSTRING_INDEX(yourColumnName,’yourDelimiter’,-1)as anyVariableName from yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下
mysql> create table LastIndexString -> ( -> Id int, -> yourURL text -> ); Query OK, 0 rows affected (0.89 sec)
使用 INSERT 命令向表中插入一些记录。查询如下 −
mysql> insert into LastIndexString values(1,'https −//www.example.com/home.html'); Query OK, 1 row affected (0.26 sec) mysql> insert into LastIndexString values(2,'https −//exampledemo.example.com/index.jsp'); Query OK, 1 row affected (0.18 sec) mysql> insert into LastIndexString values(3,'https −//www.example.com/question/LastString'); Query OK, 1 row affected (0.18 sec)
使用 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)
广告