mysql - 提取mysql中字符之间的值?

标签 mysql replace

我遇到了一个问题,我是 mysql 的初学者,我被分配创建一个从 url 地址提取值的查询。所以基本上我有数千个这样的网址地址,例如'https://www.google.com/search?source= ' 我需要提取从 '://www' 到第一个 '/'(在本例中为 'google.com')的所有内容,基本上是域名。而且没那么简单,有时是http,有时是'https://google.com/search?source= ' 没有 www 等。我已经到了这里,但我感到无助,有什么建议吗?

select distinct SUBSTRING_INDEX(col,'://',0) col from table where length(col) - length(replace(col, '://', '')) >= 1 
         and length(col) - length(replace(col, '/', '')) >= 1 
                and col = 'value'
                    and col <> ''
                      and col is not null 
                        order by date
                         limit 600;

最佳答案

您可以使用这样的查询:

SELECT 
  SUBSTRING_INDEX (
     SUBSTRING_INDEX( 'https://www.google.com/search?source=', '/',3),
 '/', -1);

示例:

MariaDB [(none)]> SELECT SUBSTRING_INDEX (SUBSTRING_INDEX( 'https://www.google.com/search?source=', '/',3) , '/', -1);
+----------------------------------------------------------------------------------------------+
| SUBSTRING_INDEX (SUBSTRING_INDEX( 'https://www.google.com/search?source=', '/',3) , '/', -1) |
+----------------------------------------------------------------------------------------------+
| www.google.com                                                                               |
+----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]>

关于mysql - 提取mysql中字符之间的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47980120/

相关文章:

javascript - 高效的 Javascript 字符串替换

javascript - 如何在不将所有 & 替换为 & 的情况下替换 href url

linux - 使用 awk 修改文本

Maven:使用程序集作为结果 Artifact -appendAssemblyId == false?

mysql - 从 MySql 中的先前记录更新新添加的字段

mysql - 同一张表内两次内连接

php - 访问每个用户的所有任务

regex - 替换 powershell 中的特殊字符

mysql - 在 R 中用查询替换表达式参数

mysql - 使用 cloudsql 在 Google Cloud 计算上构建网站