Relevance method for sorting MYSQL full-text search results
P粉938936304
2023-08-24 12:03:52
<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>
LIKE is not Full text search. In full-text search,
MATCH(...) AGAINST(...)
returns a match score that approximates relevance.Exact matches will be listed first