MySQL: selecting totals as three fields from same table as one query?

Posted by coderama on Stack Overflow See other posts from Stack Overflow or by coderama
Published on 2013-06-27T10:15:59Z Indexed on 2013/06/27 10:21 UTC
Read the original article Hit count: 181

Filed under:
|

I have a table with various orders in it:

ID | Date       | etc...
1  | 2013-01-01 | etc
2  | 2013-02-01 | etc
3  | 2013-03-01 | etc
4  | 2013-04-01 | etc
5  | 2013-05-01 | etc
6  | 2013-06-01 | etc
7  | 2013-06-01 | etc
8  | 2013-03-01 | etc
9  | 2013-04-01 | etc
10 | 2013-05-01 | etc

I want a query that ends wit the result:

overallTotal | totalThisMonth | totalLastMonth
10           | 2              | 1

But I want to do this in one query! I am trying to find a way to use subqueries to do this. SO far I have:

SELECT * from (
  SELECT count(*) as overallTotal from ORDERS
)

How can I combine this with other subqueries so I can get the totals in one query?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about sql