Mysql query to extract domains from urls

前端 未结 12 2160
小鲜肉
小鲜肉 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:47

    If you want to find the first occurrence of / why are you passing 8 into LOCATE as the starting position? (I assume to skip slashes after the protocol such as http:// but if you also expect url's without a protocol, consider short ones like cnn.com/page)

    IF(LOCATE('/', url) > 0, SUBSTRING(url, 1, LOCATE('/', url) - 1), url) AS domain
    

    Alternatively:

    SUBSTRING_INDEX(url, '/', 1) AS domain
    

    The latter seems easier to me.

提交回复
热议问题