search

Home  >  Q&A  >  body text

Relevance method for sorting MYSQL full-text search results

<p>I am relatively new to MYSQL and have a problem that has been bothering me for a while. I've tried searching for answers in various places, but haven't found an acceptable solution yet. </p> <p>This is the query I currently run to find the best match for a given search term: </p> <pre class="brush:php;toolbar:false;">$query="SELECT * from `vocabulary` WHERE translation = 'word' OR translation LIKE '%word%'";</pre> <p>The results it returns are comprehensive, including all relevant rows. However, they are not sorted in any particular order and I would like when printing the results in PHP to show the exact matches first. Like this: </p> <hr /> <p>1 | word <-exact match</p><p> 2 | crossword <-alphabetical partial match /</p><p> 3 | words</p><p> 4 | wordsmith</p> <hr /> <p>Thank you very much for your help in advance. </p> <p>-macspacejunkie</p>
P粉938936304P粉938936304501 days ago577

reply all(2)I'll reply

  • P粉090087228

    P粉0900872282023-08-25 09:40:13

    LIKE is not Full text search. In full-text search, MATCH(...) AGAINST(...) returns a match score that approximates relevance.

    reply
    0
  • P粉039633152

    P粉0396331522023-08-25 00:44:30

    SELECT * from vocabulary 
    WHERE translation like 'word'  
    union all
    SELECT * from vocabulary 
    WHERE translation LIKE '%word%' and translation not like 'word'

    Exact matches will be listed first

    reply
    0
  • Cancelreply