//--log日志记录 //log id uid fen time 1 1 10.00 2015-9-16 //今天的不用统计,原因是是定时执行 2 1 10.00 2015-8-31 3 1 10.00 2015-8-30 4 1 10.00 2015-8-30 5 2 10.00 2015-8-26 6 2 20.00 2015-8-26 7 2 10.00 2015-8-25 8 6 10.00 2015-8-22 9 6 10.00 2015-8-22 9 6 10.00 2015-8-22 //--sum汇总记录 //怎么从得出下面这个汇总的表.... //要求:通过log表汇总(保留今天的信息)插入到sum,然后删除log表的信息(保留log表今天的信息) //sum s_id s_fen log_uid s_time 1 20.00 1 2015-8-30 2 10.00 1 2015-8-31 -- 3 30.00 2 2015-8-26 4 10.00 2 2015-8-25 -- 5 30.00 6 2015-8-22
解决方案
15
汇总
select sum(fen) s_fen, uid log_uid, time s_time from table where time<sysdate() group by time, uid
删除log表数据
delete from table where time < sysdate()
select sum(fen) s_fen, uid log_uid, time s_time from table where time<sysdate() group by time, uid
删除log表数据
delete from table where time < sysdate()
30
INSERT INTO sumtbl(s_fen, log_uid, s_time) SELECT SUM(fen) AS s_fen, uid AS log_uid, `time` AS s_time FROM logtbl WHERE `time`<sysdate() GROUP BY `time`, uid -- 删除 delete from logtbl where `time` < sysdate()
s_id 字段设置成自增主键。