我有一个这样的查询,它查看大量不同的 URL,并按主机名对它们进行分组。它非常丑陋,但似乎足够快可以使用。
我如何编写它以便以更简洁的方式编写丑陋的子字符串(它抓住了域的第一部分)?我正在从一系列社交媒体网站生成查询,因此那里可能会有更多网站。
SELECT substring(r.name, 8, locate("/",substring(r.name FROM 8))-1) AS referer_domain,
count(USER) AS hits,
r.id
FROM core c,
referer r
WHERE c.site_url = 12
AND r.name LIKE '%/%'
AND c.referer = r.id
AND (substring(r.name, 8, locate("/",substring(r.name FROM 8))-1) = "www.delicious.com"
OR substring(r.name, 8, locate("/",substring(r.name FROM 8))-1) = "www.facebook.com"
OR substring(r.name, 8, locate("/",substring(r.name FROM 8))-1) = "m.facebook.com"
OR substring(r.name, 8, locate("/",substring(r.name FROM 8))-1) = "www.reddit.com"
OR substring(r.name, 8, locate("/",substring(r.name FROM 8))-1) = "twitter.com"
OR substring(r.name, 8, locate("/",substring(r.name FROM 8))-1) = "news.ycombinator.com"
GROUP BY substring(r.name, 8, locate("/",substring(r.name FROM 8))-1)
ORDER BY hits DESC