Mysql query to extract domains from urls

前端 未结 12 2149
小鲜肉
小鲜肉 2020-12-08 08:21

sorry for my english

i have this query to extract domain from urls

SELECT SUBSTRING(LEFT(url, LOCATE(\'/\', url, 8) - 1), 8) AS domain...
         


        
12条回答
  •  星月不相逢
    2020-12-08 08:42

    I tried multiple examples on this page (and some documentation) docs to make the following, annotated, version. It appears impossible with SUBSTRING_INDEX to remove subdomains without removing *.co.uk type domains.

    SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(url, 
    '?', 1), # split on url params to remove weirdest stuff first 
    '://', -1), # remove protocal http:// https:// ftp:// ...
    '/', 1), # split on path 
    ':', 2), # split on user:pass
    '@', 1), # split on user:port@
    ':', 1), # split on port
    'www.', -1), # remove www.
    '.', 4), # keep TLD + domain name
    '/', 1) 
    AS domain
    FROM ( 
        SELECT       'http://test.com' as url 
        UNION SELECT 'https://test.com' 
        UNION SELECT 'http://test.com/one' 
        UNION SELECT 'http://test.com/?huh' 
        UNION SELECT 'http://www.test1.test.com?http://ouch.foo' 
        UNION SELECT 'test.com' 
        UNION SELECT 'test.com/one'
        UNION SELECT 'test.com/one/two'
        UNION SELECT 'test.com/one/two/three'
        UNION SELECT 'test.com/one/two/three?u=http://maaaaannn'
        UNION SELECT 'http://one.test.com'
        UNION SELECT 'one.test.com/one'
        UNION SELECT 'https://www.bbc.co.uk/'
        UNION SELECT 'http://a.very.complex-domain.co.uk:8080/foo/bar'
        UNION SELECT 'postgres://user:pass@host.com:5432/path?k=v#f'
        UNION SELECT 'http://10.64.3.5/data_check/index.php?r=index/rawdatacheck'
        UNION SELECT 'two.one.test.com/one' ) AS test; 
    

提交回复
热议问题