I have two tables in my MySQL 5.5 database:
CREATE TABLE `t_user` (
`USER_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`USER_NAME` varchar(255) NOT NULL,
PRIMARY KEY (`USER_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
CREATE TABLE `t_user_activity_log` (
`USER_ID` int(10) unsigned NOT NULL,
`ACTIVITY_DATE` datetime NOT NULL,
`ACTIVITY_COUNT` int(10) unsigned NOT NULL,
PRIMARY KEY (`USER_ID`,`ACTIVITY_DATE`),
CONSTRAINT `FK_t_user` FOREIGN KEY (`USER_ID`) REFERENCES `t_user` (`USER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And I need to get result grouped by time period like:
----------------------------------------------
| Period | User1 | User2 | ***** | UserN |
----------------------------------------------
| 22.02.2012 | 12 | 12 | x | x |
----------------------------------------------
| 23.02.2012 | 7 | 3 | x | x |
----------------------------------------------
| 24.02.2012 | 0 | 0 | 0 | 0 |
----------------------------------------------
Period is changeable (HOUR, WEEK, MONTH, YEAR). It should be possible to limit query by FROM and TO dates and if no records for date is found - it should still be visible in result. Users should be selected from the list in()
.
Is it possible at all?