title: Is there an efficient way to search variable parameters in mySQL with different results?
body: <p>I have quite a complex question and due to the specifications I've had no luck finding any help with this. Scratching my head for a few months.</p>
  
  <p>I have multiple parameters however in this question I'll focus on only 3 </p>
  
  <ul>
  <li>Category ($cat)</li>
  <li>Subcategory ($sub)</li>
  <li>Genre ($gen)</li>
  </ul>
  
  <p>MySQL columns:</p>
  
  <pre><code>id | category | subcategory | genre | account | date_received | date_modified
  </code></pre>
  
  <p>(Account is assigned: 1-3)</p>
  
  <p>I would like the results to return:</p>
  
  <ul>
  <li><p>if only Category is offered:
  first the row <code>account = 1</code>, then <code>date_received</code>, followed by <code>date_modified</code> (give no preference to accounts <code>2 &amp; 3</code>) </p></li>
  <li><p>if only Category AND Subcategory is offered:
  first the row <code>account = 1</code> and <code>account = 2</code>, then <code>date_received</code>, followed by <code>date_modified</code> (give no preference to account <code>3</code>) </p></li>
  <li><p>if only Category AND Subcategory AND genre is offered:
  first the row <code>account = 1</code> and <code>account = 2</code> and <code>account = 3</code>, then <code>date_received</code>, followed by <code>date_modified</code></p></li>
  </ul>
  
  <p>So far I have this:</p>
  
  <pre><code>if ($cat AND empty($sub) AND empty($gen)) {
      $run = mysql_query("SELECT * 
                          FROM db 
                          WHERE (category='$cat') 
                          ORDER BY case when account = '1'  then 1 else 2 end,    
                              GREATEST(date_received, modified_date, account) DESC ");
  } else if ($cat AND $sub AND empty($gen)) {
      $run = mysql_query("SELECT * 
                          FROM db 
                          WHERE (category='$cat' AND subcategory='$sub') 
                          ORDER BY case when account = '2'  then 1 else 2 end,
                              GREATEST(date_received, modified_date, account) DESC 
                        ");
  } else if ($cat AND $sub AND $gen) { 
      $run = mysql_query("SELECT * 
                          FROM db 
                          WHERE (category='$cat' AND subcategory='$sub' AND genre='$gen') 
                          ORDER BY case when account = '3'  then 1 else 2 end, 
                              GREATEST(date_received, modified_date, account) DESC 
                          "); }
  </code></pre>
  
  <p>Is there an efficient way of combing these and echoing the different results?
  It feels really messy.</p>
  
  <p>Many thanks everyone for any tips you can offer.</p>