title: Query to get top 5 users by total month sales amount
body: <p>I was hoping you could help me figure this out. I would like to run a <code>query</code> that would search a table and all it's rows and the date matches the current month. Then return the <code>user_id</code>. What I am trying to achieve is gathering the top 5 user id's under this <code>query</code>.</p>
  
  <p>How can I change this to find the top 5 for the current month? I am usually able to figure out my queries but this one has me stumped.  </p>
  
  <p>This is all I have at the moment. </p>
  
  <pre><code>$top = mysqli_query($mysqli, "SELECT sum(amount) AS total FROM sales WHERE status = 'S' AND MONTH(date) = 1");
  
      while($row = mysqli_fetch_row($top))
           {
  
             $userid = $row['0'];
  
           }
  </code></pre>
  
  <p>Update:</p>
  
  <pre><code>$top = mysqli_query($mysqli, "SELECT user_id
      FROM
      (
          SELECT user_id, SUM(amount) total 
          FROM sales 
          WHERE status = 'S' 
          AND MONTH(date) = MONTH(CURDATE())
          AND YEAR(date)  =  YEAR(CURDATE())
          GROUP BY user_id
      ) q
          ORDER BY total DESC
          LIMIT 5");
  
             while($row = mysqli_fetch_row($top))
                  {
                     $topsales[] = $row['0'];
                  }
  
      foreach($topsales as $topsale) {
              echo $topsale;
      }
  </code></pre>