查询本周时间内的数据,并按每天分组

代码如下:

select a.click_date,b.count
from (
    select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 5 DAY) as click_date
        UNION ALL
        select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 4 DAY) as click_date
        UNION ALL
        select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 3 DAY) as click_date
        UNION ALL
        select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 2 DAY) as click_date
        UNION ALL
        select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 1 DAY) as click_date
        UNION ALL
        select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) DAY) as click_date
        UNION ALL
        select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 1 DAY) as click_date
) a left join (
  select date(endtime) as datetime, SUM(money) as count
  from table
  group by date(endtime)
) b on a.click_date = b.datetime;

你可能感兴趣的:(技术小例子)