生成连续的时间序列

本文介绍了一种使用SQL从当前日期生成前七天日期序列的方法,通过左连接将这些日期与注册记录表进行匹配,以获取每天的注册数量。示例中使用了MySQL的help_topic表来生成日期序列,并与zs_referrer_registration_record表中的实际注册数据进行对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

生成当前日期前七天的时间序列(见粗体部分,整体是demo)

select t1.date,ifnull(t2.count,0) count from
(
select date_format(date_add(now(),interval -t.help_topic_id day),’%Y-%m-%d’) date
from mysql.help_topic t where t.help_topic_id<7

) t1
left join
(
select date_format(create_time,’%Y-%m-%d’) date,count(id) count
from zs_referrer_registration_record group by date_format(create_time,’%Y-%m-%d’)
) t2
on t1.date=t2.date
order by t1.date

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值