This is from http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
Posted by J Soza on March 10 2005 9:28pm
Here's an example of using SUBSTRING_INDEX and TRIM to enable the grouping and counting of referring URLs, where you might have stored URLs like 'http://www.example.com/links', 'http://www.example.com/index.php?func=links', or 'http://example.com' (assuming URL storage field named 'url'):
SELECT COUNT( * ) AS total, TRIM( LEADING 'www.' FROM SUBSTRING_INDEX(TRIM( LEADING 'http://' FROM url ) ,'/', 1)) AS host FROM referers GROUP BY host ORDER BY total DESC
This will take the above-mentioned URL examples, group them as 'example.com' and give you an accurate count of how many referrals that host has generated. Tested on MySQL 4.1.10.
Read the rest of the posts too.
I hope this helps