Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

mySQL: Find most similar item

I want to find the item with the most similar characters as: 'foo'

So if these are the items:
        green
        food
        money
        pizza

the "food" should be returned.
select `NAME` from `organizations` where (NAME MOST SIMULAR TO FOO)

Open in new window

Avatar of NicoJuicy
NicoJuicy
Flag of Belgium image

You can use the LIKE sql function ;)

You can find more info here.

Good luck
select `NAME` from `organizations` where name LIKE "foo"

Forgot to mention it :)
I don't think LIKE is what you are thinking of.
LIKE would require you to use placeholders and it doesn't evaluate which is the best match.
You need to do this pro grammatically.
Look at Edit distance, Leventhstein distance, Smith-Waterman or similar solutions.

Try to find a data mining tool like Orange which includes text mining.
Avatar of hankknight

ASKER

No, LIKE is not good enough.

I want the MOST simular match for foo.

So if these are the items:
        green
        boo
        money
        pizza

"boo" should be returned.

and:
        green
        fio
        money
        pizza

"fio" should be returned.
E.g.:
PHP:
int levenshtein ( string $str1 , string $str2 );
returns the difference between the two strings.
If you do that for all strings in the table compared to the input string and take the result(s) with the smallest number you have the best match(es)
Is there anything like levenshtein available with pure sql ?
Not that I know of
SOLUTION
Avatar of allmer
allmer
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@tkalchev
I am also interested in how you would solve this in SQL.
May solve me lots of time ;)
Do you think this would work:

SELECT `NAME` from `organizations` where soundex(NAME) = soundex(foo) ??

Well apart from the limitations it sounds too good to be true.


Probably, just test it :)