如何在 MySQL 中提取 URL 的一部分?


你需要使用 MySQL 的 SUBSTRING_INDEX() 函式來提取 URL 的一部分。

 我們先建立一個表 -

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   URL text
);
Query OK, 0 rows affected (0.53 sec)

使用 insert 命令在表中插入一些記錄 -

mysql> insert into DemoTable(URL) values('https:\www.example.com\homepage');
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable(URL) values('https:\www.onlinetest.com\welcome\indexpage');
Query OK, 1 row affected (0.12 sec)

以下是使用 select 語句顯示錶格中所有記錄的查詢 -

mysql> select *from DemoTable;

這會生成以下輸出。這裡,我們只能看到一個斜線,因為 MySQL 在結果中刪除了一個斜線 -

+----+---------------------------------------------+
| Id | URL                                         |
+----+---------------------------------------------+
| 1  | https:\www.example.com\homepage             |
| 2  | https:\www.onlinetest.com\welcome\indexpage |
+----+---------------------------------------------+
2 rows in set (0.00 sec)

以下是 MySQL 中提取 URL 一部分的查詢 -

mysql> select substring_index(URL,'\',-1) from DemoTable;

這會生成以下輸出 -

+------------------------------+
| substring_index(URL,'\',-1) |
+------------------------------+
| homepage                     |
| indexpage                    |
+------------------------------+
2 rows in set (0.00 sec)

更新日期: 2019-07-30

2000+ 瀏覽量

開始你的職業

完成課程獲得認證

開始
廣告
© . All rights reserved.