0

I'm looking for an SQL function that can get the 20 most similar results. If results are completely different I still want it to fetch 20 results starting with the most similar.

The LIKE parameter appears to be looking for matches that are too exacting to the current variable and at the moment in this example query is only fetching 2 results.

$sims = mysql_query("SELECT * FROM electors 
                     WHERE constituency = '$constituency' AND ward = '$ward' 
                     AND surname LIKE '$surname'");
Cœur
  • 37,241
  • 25
  • 195
  • 267
Walrus
  • 19,801
  • 35
  • 121
  • 199
  • 1
    have u try `RLIKE` or use OR if you want any of one conditions to be matched – xkeshav Jun 27 '12 at 08:36
  • possible duplicate of [How to find similar results and sort by similarity?](http://stackoverflow.com/questions/3338889/how-to-find-similar-results-and-sort-by-similarity) – xdazz Jun 27 '12 at 08:38
  • 1
    your question is little unclear. what you want?? can you write desired results – xkeshav Jun 27 '12 at 08:38
  • 1
    mysql is terrible for doing similarity queries that include spellchecking or some kind of word-distance matching. unless you want simple x LIKE '%y%' - you have to look elsewhere(lucene, sphinx, etc) – keymone Jun 27 '12 at 08:40
  • How would you define similarity? Two strings that have the same length can be considered similar, or having the same language, the same sentence structure, the same letters, similar meaning, etc. It's fairly simple to implement a levenshtein distance function in MySQL using user-defined functions, though this isn't appropriate for all applications. So it's necessary to know what kind of similarity you're looking for. – Lèse majesté Jun 27 '12 at 08:42

2 Answers2

0

to get the "similars" in mysql you can do a FullText query (http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html)

Pablo Martinez
  • 2,172
  • 1
  • 23
  • 27
0

You can use LIKE with % which is not that strict. For example surname LIKE '%apple%' will return fields which have the apple word in the middle like pineapple, apple123 or pineapple123.

slash197
  • 9,028
  • 6
  • 41
  • 70