简体   繁体   English

MySQL SELECT REPLACE字符串的一部分

[英]MySQL SELECT REPLACE part of string

I have a column with data like this: 我有一列这样的数据:

http://www.example.com/category/link.html
http://www.example2.com/category2/link.html
http://www.example3.com/directory/category/link.php
http://www.example4.com/category4/link.php
http://www.example5.com/directory/link.html

I want to select the distinct domain.com part of these links, and then insert this into another column 我想选择这些链接中与众不同的domain.com部分,然后将其插入另一列

SELECT DISTINCT REPLACE(domain,'http://www.','') AS domain FROM table;

This selects everything without the http://www. 这将选择不带http://www. How do I remove the rest of the link so im left with domain.com and then insert this into another column? 如何删除剩下的链接,使我留在domain.com ,然后将其插入另一列?

You can do this with substring_index() . 您可以使用substring_index()进行此操作。 For instance, this might work on your data: 例如,这可能适用于您的数据:

select substring_index(substring_index(col, '/', 3), '.', -2) as domain

Try this: 尝试这个:

SELECT DISTINCT SUBSTRING_INDEX(REPLACE(domain,'http://www.',''), '/', 1) AS domain
FROM mytable

Applying SUBSTRING_INDEX after 'http://www.' 'http://www.'之后应用SUBSTRING_INDEX 'http://www.' has been eliminated from the domain string, returns the part of the string before the first occurrence of '/' . 已从域字符串中删除,返回第一次出现'/'之前的字符串部分。

Demo here 在这里演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM