Makromat
Makromat

Reputation: 1572

Query for getting comma separated data from MySQL

I still strugling with creation of mysql query where I want to do something like:

My table's name is called orders, structure is like:

id | price | kind | orderdate
-----------------------------
1  |  35   | 2    | 2013-01-01
2  |  30   | 2    | 2013-01-02
3  |  25   | 2    | 2013-01-03
4  |  15   | 2    | 2013-01-03

I don't know how to get comma separated data like: 1, 1, 2 where this numbers are count of rows in particular orderdate. So further if I'll want example "count of orders" from every day between in two monts I'll have comma separated array of 60 values. I will pass data from date array so I need to get 0 value if in particular orderdate is nothing found.

Upvotes: 0

Views: 489

Answers (1)

juergen d
juergen d

Reputation: 204904

Group by the date and then you can use count the get the number of records for every date. If you want that in a single row then use group_concat

select group_concat(count(*) separator ', ') as counts
from orders
group by orderdate
order by orderdate

Upvotes: 1

Related Questions