mysql根据开始和结束日期统计每天个数

有一个表有开始日期start_date和结束日期end_date,现在需要统计2.16-2.21每天在这个时间段的数量:如:想的到如下结果

2.16 – 2(在3,5日期范围内)
2.17 – 3(在2,3,5日期范围内)
2.18 – 5
2.19 – 5
2.20 – 4
2.21 – 3

在这里插入图片描述

SELECT a.fd_time
,COUNT(b.id) AS fd_value_plan
FROM (
	select fd_time from
	(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) AS fd_time from
	 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
	 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
	 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
	 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
	 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
	where fd_time between date_sub( CURDATE(), INTERVAL 5 DAY ) and CURDATE() 
	ORDER BY fd_time
	) a
	LEFT JOIN 
	( SELECT id,start_date,end_date FROM test ) b
	ON a.fd_time BETWEEN b.start_date and b.end_date
GROUP BY a.fd_time	
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值