title: How to order mysql search results by relevence of word priority
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/344199/php-mysql-search-and-order-by-relevancy">PHP MySQL Search And Order By Relevancy</a>  </p>
  </blockquote>
  
  
  
  <p>Hi,</p>
  
  <p>I have a table with several columns containing fields like name, address, company etc. Lets say someone search for "microsoft john". I want the results containing "microsoft" should appear first, then results containing john. vice versa if query is "john microsoft"</p>
  
  <p>My php code is:</p>
  
  <pre><code>$searchitems=explode(" ", $trimmed);
  //print_r($searchitems);
  $so = $_GET['so']=='2'?"2":"1";
  
  $clause = $so=='2'?"AND":"OR";
  include("dbconnect.php");
  // Build SQL Query
  
  $query = "select FirstName,LastName,course,Department,batch,City,companyjob,companylocation,
      companyposition,coursename,institutename,coursename2,institutename2,coursename3,
      institutename3 from alumni WHERE ";
  for($i=0;$i&lt;count($searchitems);$i++)
  {
      $queryappend .= "(FirstName LIKE '".$searchitems[$i]."%' OR LastName LIKE '".$searchitems[$i]."%'
      OR City LIKE '".$searchitems[$i]."%' OR CountryorRegion LIKE '".$searchitems[$i]."%'
      OR companyjob LIKE '".$searchitems[$i]."%' OR companylocation LIKE '".$searchitems[$i]."%'
      OR coursename LIKE '".$searchitems[$i]."%' OR institutename LIKE '".$searchitems[$i]."%'
      OR coursename2 LIKE '".$searchitems[$i]."%' OR institutename2 LIKE '".$searchitems[$i]."%')";
      if($i&lt;count($searchitems)-1) $queryappend .= $clause;
  
  }
  $query .=$queryappend;
  </code></pre>
  
  <p>The problem is MYSQL is ordering the results by id... This makes it funny, because some higher valued results may be stuck deep in the stack. btw, phpmyadmin search has the same flaw. </p>
  
  <p>Please suggest.</p>