Display more sales order by staff first?

SELECT * FROM sales WHERE submit_date >= 1277074800 AND submit_date <= 1277679600

It output like this:

3, Adam, Date_Submit
4, Paul, Date_Submit
5, Adam, Date_Submit
6, John, Date_Submit
7, Paul, Date_Submit
8, Adam, Date_Submit

I wanted to output which staff has the most sales come first, like this this:

3, Adam, Date_Submit
5, Adam, Date_Submit
8, Adam, Date_Submit
4, Paul, Date_Submit
7, Paul, Date_Submit
6, John, Date_Submit

As you can Adam has 3, Paul has 2, and John has as 1. How to display in this way?

You counted the sales each user made EVER, not only in the GIVEN PERIOD. That doesn’t mean it’s wrong, it all depends on what you want.

I did order by TotalUser… not user.

You order by the users that have the most sales ever, not the most sales in the given period.

And I’ll leave it to Rudy to tell you about the INNER JOIN :wink:

Thanks r937.

I have manged to this way:

SELECT * FROM sales f,
  (select username, count(*) as TotalUser from sales group by username) as f2  
  where f.username=f2.username  and 
  submit_date >= $StartMon AND submit_date <= $EndSun 
  order by TotalUser DESC";

Isn’t that better and shorter?

SELECT sales.id
     , sales.staffname
     , sales.submit_date
  FROM ( SELECT staffname
              , COUNT(*) AS lines
           FROM sales
          WHERE submit_date >= 1277074800  
            AND submit_date <= 1277679600
             BY staffname ) AS totals
  JOIN sales
    ON sales.staffname = totals.staffname
 WHERE sales.submit_date >= 1277074800  
   AND sales.submit_date <= 1277679600
    BY totals.lines DESC
     , sales.submit_date

There is no field for number of sales. To know the number of sales, the lines for each employee have to be counted.

Can you tell us the field names for number of sales and employee name.

at least tell us what is name of table field for emplyee?