2

I have a table tracking links structured as follows:

id
domain_name
traffic_count
enabled
created_at

Let's say I have a record with domain_name of "some-domain.com", and then let's say I want to find the record having that domain, except all I have to go by is a subdomain of that domain.

I essentially want to do this:

SELECT * FROM links 'subdomain.some-domain.com' LIKE %domain_name%"

How would I do this?

1
  • Which DB engine are you using? MySQL? Commented Mar 1, 2012 at 20:50

1 Answer 1

11

Updated (thanks to Chad Johnson):

SELECT * FROM links WHERE 'subdomain.some-domain.com' LIKE CONCAT('%', domain_name, '%')
Sign up to request clarification or add additional context in comments.

4 Comments

Shouldn't there be a where clause?
Very close! Looks like this works: SELECT * FROM links WHERE 'subdomain.some-domain.com' LIKE CONCAT('%', domain_name, '%')
@ChadJohnson, yes, that's why I asked which DB. On MySQL you have to use CONCAT. Also, you need a WHERE, like SELECT * FROM links WHERE (etc).
@ChadJohnson Note of caution, this query won't be able to use indexes or even worse (as SQL Server seems to do) builds a temporary index in memory, answers the question and throws the index away. In other words, don't use on a high traffic site without testing performance thoroughly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.