Here is an example what I am trying to do:
I have to organize a list of events by month/year with the count of the events, then a comma list of the IDs of those events.
Here is what I have:
SELECT count(*) as counter,
MONTHNAME(publishDate) as month,
YEAR(publishDate) as year
FROM bursch.events
GROUP BY YEAR(publishDate),
MONTH(publishDate)
order by year desc;
BUT, in addition, I need those IDs too (2,5,6) <<< like this:
Here is the concept:
SELECT count(*) as counter,
MONTHNAME(publishDate) as month,
YEAR(publishDate) as year,
(select id
from events
where 'call conditions affect the main query') as IDList
FROM events
GROUP BY YEAR(publishDate),
MONTH(publishDate)
order by year desc;
Which would result in something like this:
counter | month | year | ids
-----------------------------------------
3 | June | 2013 | 45,49,50
4 | July | 2013 | 39,40,41,42
2 | March | 2011 | 33,34
5 | May | 2011 | 27,29,30,31,32
1 | June | 2011 | 22
4 | July | 2011 | 14,17,18,19
1 | January | 2010 | 13
It would be great of I can base my select results on the conditions of the main select statement. There may be a better way to get this result. Maybe a simpler method. But the select as field part. What would that be called? Correlated query? Not sure. I have seen that I can do that in Oracle and MySQL and it comes in handy (if I only knew what it is called).
Thanks in advanced. Let me know if something is unclear.
MySQL has a function called GROUP_CONCAT() which concatenates rows instead of columns.
SELECT COUNT(*) as counter,
MONTHNAME(publishDate) as month,
YEAR(publishDate) as year,
GROUP_CONCAT(id) as IDs
FROM events
GROUP BY YEAR(publishDate),
MONTH(publishDate)
ORDER BY year desc;