• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 527
  • Last Modified:

MySQL query, get the closest if exact not found


How do I write a query that returns the closest / best matches if exact is not found? The best implementation can be seen on google.com that says "Did you mean _______" Now a days they have changed it and show the result for the correct keyword by default and shows "Search instead for ________" below it.

E.g. searching for:
fung shui

or any other spelling variation always results in correct one i.e. feng shui

1 Solution
Jim P.Commented:
Basically you would have to build up an index table that has the correct word and the variants that are off. Then you also have have to build a probability function so if someone were to type in "italian piza seattle" that you feed back the says "Did you mean pizza, Seattle Washington?" versus "Did you mean Pisa Italy?"
practitionerAuthor Commented:
Thanks Tomas, Soundex function is the key.

Thanks jimpen, building an index is very difficult, although soundex is the implementation I need at the moment, I would love to know the best approach to build such an index automatically, I believe Google wouldn't have done this manually because it's almost impossible.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now