I have this table from which I need to select visitors count by date.
CREATE TABLE `visitors` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`ip` VARCHAR(32) NOT NULL,
`browser` VARCHAR(500) NOT NULL,
`version` VARCHAR(500) NOT NULL,
`platform` ENUM('w','l','m') NOT NULL,
`date` DATE NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
I try this:
SELECT DATE(`date`) AS `date`, COUNT(*) AS `visitor`
FROM `visitors` WHERE (`date` >= (NOW() - INTERVAL 7 DAY))
GROUP BY `date`;
Which returns this:
date visitor
2015-12-19 2
2015-12-22 5
But what I really want is this:
date visitor
2015-12-17 0 // Make 0 for the day which is not present
2015-12-17 0 // Make 0 for the day which is not present
2015-12-18 0 // Make 0 for the day which is not present
2015-12-19 2
2015-12-20 0 // Make 0 for the day which is not present
2015-12-21 0 // Make 0 for the day which is not present
2015-12-22 5
If I try with unions and get dates with this it is not problem with example 1 week. But what can I do if I need 1 year?