如何在 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.