php - Matching multiple similar strings in SQL -
so here situation:
i need find way match 2 similar strings, example:
a - samsung galaxy s5 white
b - mobile phone samsung galaxy s5 white
i have string a
in database field, have , equivalent string coming outside source. is, same product, if straight search title field, search not yield desired results.
any ideas how make lookup, similar strings? maybe break string tokens? there query write according tokens? ngram lookup feature introduced in mysql 5.7.6 useful in scenario?
any other suggestions?
using like
being described in other answers won't work, because text you're searching not substring of what's in database (given example). there couple of ways handle this, depending on situation.
if know in advance different varying strings might supplied with, can create table aliases
store strings , link them primary table foreign key.
if don't know them in advance, perhaps because user-supplied search terms, need dynamically build query break terms , search them individually, this:
select ... table field '%mobile%' or field '%phone%' or field '%samsung%' or field '%galaxy%' or field '%s5%' or field '%white%'
while find correct result, may return false positives (i.e. word "white" or "phone" returned), not way go.
if you're using myisam tables mysql <= 5.5, or myisam or innodb tables mysql >= 5.6, can use full-text searches , match/against, this:
select match( field ) against ( 'mobile', 'phone', 'samsung', 'galaxy', 's5', 'white' ) relevance table match( field ) against ( 'mobile', 'phone', 'samsung', 'galaxy', 's5', 'white' in boolean mode) order relevance desc
this not find rows match, sort them relevance (i.e. how many match). limit 1 row if knew relevant match correct one, or display list of choices beginning relevant.
check out documentation page full-text search functions more info.
Comments
Post a Comment