Mysql Query查找重复的url附近

时间:2014-11-03 22:07:15

标签: mysql subquery

我试图消除mysql数据库表中的重复域名(URL' s。)

我已经使用此查询来查找"相同"网址:

SELECT URL, 
COUNT(*) c 
FROM Links 
GROUP BY URL 
HAVING c > 1;

但是这个查询无法找到我需要的相同域名的不同网址:

example.com
www.example.com
www.example.com/
www.example.com/somepage.htm

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:1)

您可以非常轻松地处理最后三种情况:

select min(url), count(*) as c
from links
group by substring_index(url, '/', 1)
having c > 1;

要获得第一个,我建议在字符串的开头删除www.。以下内容应该有效(如果.www稍后在第一个/之前的网址中出现,则会失败:

select min(url), count(*) as c
from links
group by (case when url like 'www.%' then substring(substring_index(url, '/', 1), 5)
               else substring_index(url, '/', 1)
           end)
having c > 1;
相关问题