title: MySQL order by "best match"
body: <p>I have a table that contains words and an input field to search that table using a live search. Currently, I use the following query to search the table: </p>
  
  <pre><code>SELECT word FROM words WHERE word LIKE '%searchstring%' ORDER BY word ASC
  </code></pre>
  
  <p>Is there a way to order the results so that the ones where the string is found at the beginning of the word come first and those where the string appears later in the word come last?</p>
  
  <p>An example: searching for '<strong>hab</strong>' currently returns </p>
  
  <ol>
  <li><strong>a</strong> lphabet</li>
  <li><strong>h</strong> abit</li>
  <li><strong>r</strong> ehab</li>
  </ol>
  
  <p>but I'd like it this way:</p>
  
  <ol>
  <li><strong>hab</strong> it      (first because 'hab' is the beginning)</li>
  <li>alp <strong>hab</strong> et  (second because 'hab' is in the middle of the word)</li>
  <li>re <strong>hab</strong>      (last because 'hab' is at the end of the word)</li>
  </ol>
  
  <p>or at least this way:</p>
  
  <ol>
  <li><strong>hab</strong> it  (first because 'hab' is the beginning)</li>
  <li>re <strong>hab</strong>  (second because 'hab' starts at the third letter)</li>
  <li>alp <strong>hab</strong> et  (last because 'hab' starts latest, at the fourth letter)</li>
  </ol>
  
  <p>Would be great if anyone could help me out with this! </p>