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)

更新於: 30-Jul-2019

667 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.