views:

1012

answers:

4

I have a MySQL table containing domain names:

+----+---------------+
| id | domain        |
+----+---------------+
|  1 | amazon.com    |
|  2 | google.com    |
|  3 | microsoft.com |
|    |     ...       |
+----+---------------+

I'd like to be able to search through this table for a full hostname (i.e. 'www.google.com'). If it were the other way round where the table contained the full URL I'd use:

SELECT * FROM table WHERE domain LIKE '%google.com%'

But the inverse is not so straightforward. My current thinking is to search for the full hostname, then progressively strip off each part of the domain, and search again. (i.e. search for 'www.google.com' then 'google.com')

This is not particular efficient or clever, there must be a better way. I am sure it is a common problem, and no doubt easy to solve!

+5  A: 

You can use the column on the right of the like too:

SELECT domain FROM table WHERE 'www.google.com' LIKE CONCAT('%', domain);

or

SELECT domain FROM table WHERE 'www.google.com' LIKE CONCAT('%', domain, '%');

It's not particularly efficient but it works.

Greg
that's so awful it's cool :-)
Orion Edwards
Good or evil... they're both fine choices.
Greg
Cool. I had thought about putting the % with the column name, but didn't know how to concatenate like that. Thanks.
Mat
A: 

Sorry, this isn't an answer, just a request for clarification.

Do you want to match www.google.com, but not google.com? Because %.google.com% will match all, I think.

Eli
You might want to remove that leading period. Otherwise, no, it wouldn't match www.google.com and google.com...
Kevin Fairchild
Pardon, should have said all but google.com. I think he may just be looking for those with subdomains. Whatever, I am confoosled as to the question.
Eli
I think, if he puts in 'www.google.com', he wants to match 'google.com'.
GalacticCowboy
To clarify, what I was after was to match 'google.com', 'www.google.com', 'mail.google.com'. The idea being that the subdomain would be ignored. But the leading period is useful if wanted to differentiate.
Mat
A: 

You could use a bit of SQL string manipulation to generate the equivalent of string.EndsWith():

SELECT * FROM table WHERE  
substring('www.google.com',
len('www.google.com') - len([domain]) ,
len([domain])+1) = [domain]
Radu094
+1  A: 

In mysql you can use regular expressions (RLIKE) to perform matches. Given this ability you could do something like this:

SELECT * FROM table WHERE 'www.google.com' RLIKE domain;

It appears that the way RLIKE has been implemented it is even smart enough to treat the dot in that field (normally a wildcard in regex) as a literal dot.

MySQL's inclusion of regular expressions gives you a very powerful ability to parse and search strings. If you would like to know more about regular expressions, just google "regex". You can also use one of these links: http://en.wikipedia.org/wiki/Regular_expression http://www.regular-expressions.info/ http://www.codeproject.com/KB/string/re.aspx

Chris