0

I found a similar question on this page Mysql query to extract domains from urls

SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(target_url, '/', 3), '://', -1), '/', 1), '?', 1) AS domain

But the result of this code is not correct

'www.abc.com' 'lalala.one.google.com' 'two.one.test.com'

I need to get the 2 last words, separator is dot.

I need this result

'abc.com' 'google.com' 'test.com'

0

2 Answers 2

3
select SUBSTRING_INDEX('aaa.bbb.sss.google.com','.',-2);
Sign up to request clarification or add additional context in comments.

1 Comment

This worked well to extract the basedomain from a sub-domain.
2

With subtring() and substring_index():

set @url = 'https://stackoverflow.com/questions/57937363/how-to-remove-subdomain-from-url-in-mysql';
select substring_index(substring_index(substring(@url, locate('://', @url) + 3), '/', 1), '.', -2) as domain

See the demo.
Result:

domain
-----------------
stackoverflow.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.