MySQL中统计一天之内每个时间段内的数据,很急
有两个表表一:粉丝表Fans表二:粉丝统计表Fanstrack要求Fanstrack统计从fans表里面拿取数据Total(subscribe=1)的所有的粉丝数据的总和...
有两个表
表一:粉丝表Fans
表二:粉丝统计表Fanstrack
要求
Fanstrack 统计 从fans表里面拿取数据
Total (subscribe=1)的所有的粉丝数据的总和
New (subscribe=1) 所有新增粉丝数据的总和
outflow (subscribe=0) 所有流失流失粉丝数据的总和
net 用new - outflow 净增粉丝的总和
(subscribe=1)关注的粉丝
(subscribe=0)取消关注的粉丝
(uid) 被关注的对象
按时间段(两个小时一个时间段)进行统计,定时插入到粉丝统计表里面,如果按天统计的话,通过存储过程和定时器可以实现,我这里有个sql语句
INSERT INTO yht_fanstrack(total,new,outflow,net,time,uid)
SELECT c.total,a.new,b.outflow,(a.new-b.outflow) AS net,a.time,a.uid FROM
(SELECT now() AS time,COUNT(subscribe_time) AS new,uid FROM yht_fans WHERE subscribe_time>=UNIX_TIMESTAMP(CURDATE()) and subscribe_time<=UNIX_TIMESTAMP(CURDATE())+86400 GROUP BY uid) a
JOIN
(SELECT now() AS time,COUNT(cancel_time) AS outflow,uid FROM yht_fans WHERE cancel_time>=UNIX_TIMESTAMP(CURDATE()) and cancel_time<=UNIX_TIMESTAMP(CURDATE())+86400 GROUP BY uid) b
ON
a.uid=b.uid
JOIN
(SELECT COUNT(subscribe) AS total,uid FROM yht_fans GROUP BY uid) c
ON
a.uid=c.uid
现在的问题是,按时间段先用sql语句把数据统计出来,然后每天定时自动插入,不知道如何实现。
有没哪位大神给个思路,或者写个实现方案,小弟在此谢过了!!! 展开
表一:粉丝表Fans
表二:粉丝统计表Fanstrack
要求
Fanstrack 统计 从fans表里面拿取数据
Total (subscribe=1)的所有的粉丝数据的总和
New (subscribe=1) 所有新增粉丝数据的总和
outflow (subscribe=0) 所有流失流失粉丝数据的总和
net 用new - outflow 净增粉丝的总和
(subscribe=1)关注的粉丝
(subscribe=0)取消关注的粉丝
(uid) 被关注的对象
按时间段(两个小时一个时间段)进行统计,定时插入到粉丝统计表里面,如果按天统计的话,通过存储过程和定时器可以实现,我这里有个sql语句
INSERT INTO yht_fanstrack(total,new,outflow,net,time,uid)
SELECT c.total,a.new,b.outflow,(a.new-b.outflow) AS net,a.time,a.uid FROM
(SELECT now() AS time,COUNT(subscribe_time) AS new,uid FROM yht_fans WHERE subscribe_time>=UNIX_TIMESTAMP(CURDATE()) and subscribe_time<=UNIX_TIMESTAMP(CURDATE())+86400 GROUP BY uid) a
JOIN
(SELECT now() AS time,COUNT(cancel_time) AS outflow,uid FROM yht_fans WHERE cancel_time>=UNIX_TIMESTAMP(CURDATE()) and cancel_time<=UNIX_TIMESTAMP(CURDATE())+86400 GROUP BY uid) b
ON
a.uid=b.uid
JOIN
(SELECT COUNT(subscribe) AS total,uid FROM yht_fans GROUP BY uid) c
ON
a.uid=c.uid
现在的问题是,按时间段先用sql语句把数据统计出来,然后每天定时自动插入,不知道如何实现。
有没哪位大神给个思路,或者写个实现方案,小弟在此谢过了!!! 展开
2个回答
展开全部
时间段统计,可以采用 hour(subscribe_time) 取出旦唯小时然后分层。思路:
select
uid
,CASE WHEN HOUR(subscribe_time) BETWEEN 0 AND 1 THEN '00:00:00'
WHEN HOUR(subscribe_time) BETWEEN 2 AND 3 THEN '02:00:00'
...
ELSE '23:00:00' END -- 生成时间分老迟稿层部分,insert前外层sql加上日期后作为唯一侍孝的时间值
,COUNT(*)
FROM yht_fans WHERE subscribe_time>=UNIX_TIMESTAMP(CURDATE()) and subscribe_time<=UNIX_TIMESTAMP(CURDATE())+86400
GROUP BY 1,2
select
uid
,CASE WHEN HOUR(subscribe_time) BETWEEN 0 AND 1 THEN '00:00:00'
WHEN HOUR(subscribe_time) BETWEEN 2 AND 3 THEN '02:00:00'
...
ELSE '23:00:00' END -- 生成时间分老迟稿层部分,insert前外层sql加上日期后作为唯一侍孝的时间值
,COUNT(*)
FROM yht_fans WHERE subscribe_time>=UNIX_TIMESTAMP(CURDATE()) and subscribe_time<=UNIX_TIMESTAMP(CURDATE())+86400
GROUP BY 1,2
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
今至电子科技有限公司
2024-08-23 广告
2024-08-23 广告
数据库备份是确保数据安全与业务连续性的关键环节。我们上海今至电子科技有限公司高度重视数据保护,定期执行全面的数据库备份策略。这包括使用先进工具和技术,对关键业务数据进行自动化备份,并存储在安全可靠的外部存储介质或云端。通过定期验证备份的完整...
点击进入详情页
本回答由今至电子科技有限公司提供
展开全部
@param pay_time数据库时间戳字孙困段
@param price_transaction数据库价格字段
$date = '2018-09-21';
$sql = "
SELECT
HOUR (FROM_UNIXTIME(pay_time)) AS Hour,
sum(price_transaction) as sum
FROM
zt_order
WHERE
FROM_UNIXTIME(pay_time, '%Y-%m-%d') = '$date'
GROUP BY
HOUR (FROM_UNIXTIME(pay_time))
ORDER BY
HOUR (FROM_UNIXTIME(pay_time))
";
$query = $this->db->棚唤query($sql);
$data = $query->result_array();
返回每个小时对应的和
不则和念足的地方在于如果该时间段数据为0,则不返回数据
@param price_transaction数据库价格字段
$date = '2018-09-21';
$sql = "
SELECT
HOUR (FROM_UNIXTIME(pay_time)) AS Hour,
sum(price_transaction) as sum
FROM
zt_order
WHERE
FROM_UNIXTIME(pay_time, '%Y-%m-%d') = '$date'
GROUP BY
HOUR (FROM_UNIXTIME(pay_time))
ORDER BY
HOUR (FROM_UNIXTIME(pay_time))
";
$query = $this->db->棚唤query($sql);
$data = $query->result_array();
返回每个小时对应的和
不则和念足的地方在于如果该时间段数据为0,则不返回数据
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询