Johnczek Johnczek - 3 years ago 238
MySQL Question

Parse SQL results by month using timestamp MySQL

I have a small problem with SQL. I need to select ID of rows and group them into arrays (or something) BY MONTH? I have a timestamp column there.
So if there are rows like this:

ID Timestamp
1 blalba(1.10.2017)
2 blabla(2.10.2017)
3 blabla(1.5.1996)

The output would be like

[5.1996] => array([3]),
[10.2017] => array([1,2]);

(Or something like this).
Is this possible in PHP using some PHP libraries? Or Do I have to implement my own class doing this?

Answer Source

You are probably looking for group_concat

select group_concat(id separator ', ') as myList,
DATE_FORMAT(Timestamp, '%Y-%m') from <YOUR_TABLE>
GROUP BY DATE_FORMAT(Timestamp, '%Y-%m');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download