0
Table users :
id      (int)
status  (int)
regdate (datetime)

I want to get the results in order of regdate, with count(id) 's and group by status.

For example :

 Date           Count(status = 3)    Count(status = 4)   Count(status = 5)
2014-02-24 2 5 8 2014-02-25 2 5 8

We should have get the results in only a line per day.

Thanks too much in advance.

1
  • 2
    that's called a pivot query, and mysql doesn't support them directly. the workarounds are ugly and get EXTREMELY unmaintainable VERY quickly. Fetch the query normally and do the col->row transformation in your client-side code. Commented Feb 28, 2014 at 20:59

1 Answer 1

2

You can use SUM() with condition if there are limited statutes like 3,4,5,the expression in SUM() are evaluated as boolean , for n no. of statutes look at Marc B's comments

SELECT
regdate,
SUM(status = 3),
SUM(status = 4),
SUM(status = 5)
FROM `table`
GROUP BY regdate
ORDER BY regdate
Sign up to request clarification or add additional context in comments.

1 Comment

@vural can you add the sql fiddle example of your table definitions and sample dataset

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.