CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime(6) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
insert into t1 values (null, now());
insert into t1 values (null, now());
insert into t1 values (null, now());
insert into t1 values (null, now());
insert into t1 values (null, now());
insert into t1 values (null, now());
show create table t1;
| t1 | CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime(6) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 |
> insert into t1 values (25, now());
> show create table t1;
| t1 | CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime(6) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 |
insert into stat_table select date_sub(current_date, 1), sum(sum) from table where date < current_date;
create table stat_table (
to_day date,
big_sum int,
primary key(to_day)
)
set @prev_day_sum:=0;
select
day,
@prev_day_sum:=d_sum + @prev_day_sum as sum_from_month_begin,
d_sum
from
(select
date(date) as day,
sum(sum) as d_sum
from
table
group by
day;
) as day_sum;
delimiter //
CREATE TRIGGER ins_logtable AFTER INSERT ON logtable
FOR EACH ROW
BEGIN
SELECT NOW() - interval 2 month INTO @rotate_date;
DELETE FROM logtable WHERE added_date < @rotate_date;
END;//
delimiter ;