0

I have a table which has companies (id, name). Before I had to search the input query against records which was handled with "like". Sometime client wanted it to end with the input query sometime it could occur in between sometime it should start with it. So I used different variation of "like"

Input Query: Micro

select * from Company where CompanyName like "Micro%";
select * from Company where CompanyName like "%Micro%";
select * from Company where CompanyName like "%Micro";

Pretty simple right. Now the variation started to come which were not that easy to handle with "like". Like one case occurred to search the query in starting of the words. Like for same above input query the result set should contain

Microcompany Something
Something Somethin Microcompany
Something Microcompany

So i switched to using Regular Expressions

Now my Query turned into

select * from company where CompanyName regexp '[[:<:]]Micro';

Question: Now I want the result sorted on the closest matching index. I mean the one on which match occurred on index closer to 0 will come first and then the others. For the above search result it should be order as follow.

Microcompany Something
Something Microcompany
Something Somethin Microcompany

Is it achievable through MySQL. If yes than how? and what are the alternatives?

2
  • 1
    ORDER BY LOCATE('Micro', CompanyName)? Though I'd suggest that you also look into fulltext search, or else an engine like Apache Solr or Sphinx, since your current pattern matching searches aren't sargable. Commented Mar 25, 2015 at 10:01
  • Yeah I have set up Apache Solr because beside this case there is a lot of search which is getting more and more complicated by day. But for the time being i think ORDER BY LOCATE('Micro', CompanyName) will work. Thanks Commented Mar 25, 2015 at 10:19

2 Answers 2

1

Although there may be exceptions where the following doesn't work, it is probably good enough for most cases:

select *
from company
where CompanyName regexp '[[:<:]]Micro'
order by instr( CompanyName, 'Micro');

That is, only match names where the "Micro" starts a word in the name. Then return them by the order of how close "Micro" is to the front of the word. I doubt there are many names that have "Micro" twice in the name -- and if they do, there is a good chance that each occurrence starts a word anyway.

Sign up to request clarification or add additional context in comments.

2 Comments

How about ORDER BY LOCATE('Micro', CompanyName). Isn't it the same?
@planet260 . . . Should be, and there is also the function position().
0
select * from company where instr( CompanyName, 'Micro' ) > 0 order by instr( CompanyName, 'Micro' );

Exclude entries that dont contain the search phrase (index == 0). Order by character index of the first match.

1 Comment

The search case is query in starting of the words.

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.