database - Mysql return count for last 7 days -
i having trouble run code below count rows grouped date returns wrong count each days
select date(date) days ,coalesce(count(credits),0) credits offer_process qa left join ( select date(now())-interval 30 day day_series from(select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 ) ) qb on date(date) = date(date) date > date_sub(curdate(), interval 7 day) group date(date) order date(date) asc;
insted of returning 1 each rows example, returns 7, not true
what one. return count of credits selected id each of last 7 days.
select (date(now()) - interval `day` day) `daydate`, count(`credits`) `credits` ( select 0 `day` union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 ) `week` left join `offer_process` on date(`date`) = (date(now()) - interval `day` day) , `id` = 50 group `daydate` order `daydate` asc
here example: http://sqlfiddle.com/#!9/3a6592/1
Comments
Post a Comment