Lets say I have a user table which looks like
userID int(4) unsigned not null auto_increment,
name varchar(50) not null,
date_start int(10) unsigned not null,
date_stop int(10) unsigned not null
I want to count all users that were active (between date_start and date_stop, these are unix_timestamps()) per month.
So the result should be something like this :
2012/01 55
2012/02 58
2012/03 51
I'd say something like this, but obviously I'm missing something :
SELECT
DATE_FORMAT(???, '%Y/%m'),
COUNT(userID)
FROM users
WHERE
??? BETWEEN date_start AND date_stop
GROUP BY DATE_FORMAT(???, '%Y%m');
... just to clarify, what worked for me was :
SELECT m.yearMonth, COUNT(u.userID)
FROM users u
LEFT JOIN months m ON m.yearMonth BETWEEN DATE_FORMAT(FROM_UNIXTIME(u.date_start), '%Y%m') AND DATE_FORMAT(FROM_UNIXTIME(u.date_stop), '%Y%m')
GROUP BY m.yearMonth;
Using a table that has "all" year/months stored in the form of yyyymm.