显示多次出现的修改后的字符串

时间:2013-10-14 09:11:06

标签: mysql sql

我收到了一个查询,它会切断每一行的域名后缀,例如: google.com - > google或google.co.uk - >谷歌

查询如下

SELECT id,domain, 
CASE 
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 1 THEN REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 2 THEN REVERSE(SUBSTRING(REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))), LOCATE('.', REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000)))) + 1, 1000))

END as Keydomain
FROM sites

现在我想显示多次出现的所有修改过的域名。我怎样才能做到这一点?谢谢你帮助我;)

1 个答案:

答案 0 :(得分:0)

添加

GROUP BY Keydomain
HAVING COUNT(*) > 1

查询。

修改

  

您能否告诉我是否有办法逐个列出完整的域名?

SELECT * FROM
(
SELECT 
CASE 
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 1 THEN REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 2 THEN REVERSE(SUBSTRING(REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))), LOCATE('.', REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000)))) + 1, 1000))
END as Keydomain
FROM sites
GROUP BY Keydomain
HAVING COUNT(*) > 1
) d1
INNER JOIN
(
SELECT id, domain,
CASE 
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 1 THEN REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 2 THEN REVERSE(SUBSTRING(REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))), LOCATE('.', REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000)))) + 1, 1000))
END as Keydomain
FROM sites
) d2
ON d1.Keydomain = d2.Keydomain