title: ORDER BY COUNT with condition
body: <p>I have two tables, <code>users</code> and <code>clients_closed_1</code>.</p>
  
  <p>I need to order the result by the count of the rows on the table <code>client_closed_1</code> where meeting=1. </p>
  
  <p>I did it for <code>time_closed</code> field but that was easy because there was no condition.</p>
  
  <p>It's a part of a search code so I'll show you all of it.
  With this code I manage to order it by meeting - but users who has no rows with meeting=1 isn't pull out from the database and I need them to show even if they doesn't have meetings.</p>
  
  <pre><code>if (project_type($_GET['project']) == 1)    {
      $table = 'clients_closed_1';
  }   else    {
      $table = 'clients_closed_2';
  }
      $s_query = "SELECT *,COUNT(time_closed) as numc FROM `".$table."` FULL JOIN `users` ON users.ID=user_c WHERE 1=1";
      if (isset($_POST['search']))    {   
          if ($_POST['tm'] == 'da')   {
                  $dd = strtotime($_POST['y']."-".$_POST['m']."-".$_POST['d']);
                  $s_query = $s_query." &amp;&amp; DATE(FROM_UNIXTIME(time_closed)) = DATE(FROM_UNIXTIME(".$dd."))";
          }   
          elseif ($_POST['tm'] == 'mon')  {
              $s_query = $s_query." &amp;&amp; YEAR(FROM_UNIXTIME(time_closed))=".$_POST['y']." &amp;&amp; MONTH(FROM_UNIXTIME(time_closed))=".$_POST['m'];
          }
          if (!empty($_POST['search_name']))  {
              $s_query = $s_query." &amp;&amp; CONCAT(p_name,' ',l_name) LIKE '%".$_POST['search_name']."%'";
          }
          if (!empty($_POST['level']))    {
              $query = "&amp;&amp; (level=3 &amp;&amp; project IN (SELECT `project` FROM `project` WHERE type='2')) || level=4";
          }
      }   else    {
          $s_query = $s_query." &amp;&amp; YEAR(FROM_UNIXTIME(time_closed))=YEAR(NOW()) &amp;&amp; MONTH(FROM_UNIXTIME(time_closed))=MONTH(NOW())";
      }
  
  if (isset($_GET['order']))  {
      if ($_GET['order'] == 'closing')    {
          $s_query = $s_query." GROUP BY users.ID ORDER BY numc DESC";
      }
      elseif ($_GET['order'] == 'meeting')    {
          $s_query = $s_query." &amp;&amp; meeting='1' GROUP BY users.ID ORDER BY numd DESC";
      }
  }
  $query = $db-&gt;query($s_query);
  </code></pre>
  
  <p>If you need any more code/doedn't understand something comment please and I'll fix it.</p>
  
  <p>Thank you. </p>
  
  <p>EDIT:
  example of <code>$s_query</code>:</p>
  
  <pre><code>   SELECT *,COUNT(time_closed) as numc, COUNT(meeting) as numd FROM `clients_closed_1`
     FULL JOIN `users` ON users.ID=user_c WHERE 1=1 &amp;&amp; 
      YEAR(FROM_UNIXTIME(time_closed))=YEAR(NOW()) &amp;&amp; 
      MONTH(FROM_UNIXTIME(time_closed))=MONTH(NOW()) 
      GROUP BY users.ID ORDER BY numc DESC
  </code></pre>