Reputation: 936
I have a coding problem and was wondering if there is a SQL implementation of the following scenario:
I want to search in a text column for certain words and want to sort the results based on the number of individual words found. For example:
Let's find the row which contains: a b s
a b b c d e s
b d s w d a s
x d s g w d s
f e h w d s a
The desired result would be:
a b b c d e s (it contains all 3 words)
b d s w d a s (it contains all 3 words)
f e h w d s a (it contains 2 words)
x d s g w d s (it contains 1 word)
Is it recommended doing something like this rather in e.g. PHP or is there an efficient SQL way to do so?
Upvotes: 1
Views: 359
Reputation: 1269483
You can do this using boolean expressions:
select t.*,
( (col like '%a%') +
(col like '%b%') +
(col like '%s%') +
) as num_matches
from t
order by num_matches desc;
If words should be separated by spaces, then:
select t.*,
( (concat(' ', col, ' ') like '% a %') +
(concat(' ', col, ' ') like '% b %') +
(concat(' ', col, ' ') like '% s %') +
) as num_matches
from t
order by num_matches desc;
Finally, if you have this type of problem, then you should do one of two things:
Upvotes: 1
Reputation: 467
My idea:
Upvotes: 0
Reputation: 118
If done within SQL, I would look at doing it using full text search which allows for RANK(http://msdn.microsoft.com/en-us/library/cc879245.aspx)
Upvotes: 0