Mysql 日期计算及Job相关
1. mysql 日期操作 增减天数、时间转换、时间戳(转换)
https://www.cnblogs.com/kelelipeng/p/12883609.html
2. Mysql 日期计算相关
https://www.runoob.com/sql/func-date-format.html
3. 详解 MySQL 的计划任务
https://www.cnblogs.com/brady-wang/p/6386973.html
4. mysql 定时任务 每月_mysql 定时任务 每月15号执行
https://blog.csdn.net/weixin_39704374/article/details/113228102
#查看当前是否已开启事件调度器 如果显示 on 证明已经开启 如果显示off 证明是关闭状态 show variables like 'event_scheduler'; #要想保证能够执行event事件,就必须保证定时器是开启状态,默认为关闭状态 set global event_scheduler =1; #或者 set GLOBAL event_scheduler = ON; # 如果原来存在该名字的任务计划则先删除 drop event if exists create_bill; #每月15号结算上个月的所有订单 计算上个月所有店铺的统计数据 和 单个店铺的数据统计 #select subdate(curdate(),date_format(curdate(),'%e')); 前一月最后一天 【如:2015-08-31】 #SELECT subdate(date_sub(curdate(),interval 1 month), date_format(date_sub(curdate(),interval 1 month),'%e')-1); 前一月第一天【如:2015-08-01】 DELIMITER ;; create event create_bill on schedule every 1 month starts '2018-03-15 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN INSERT INTO uc_bill (ub_number, ub_start_date, ub_end_date, strd_id, settlement_date, settlement_money, order_num, product_money, settlement_state, create_date, update_date) select (select date_format(curdate(),'%Y%m')-1) as ub_number, (SELECT subdate(date_sub(curdate(),interval 1 month), date_format(date_sub(curdate(),interval 1 month),'%e')-1)) as ub_start_date, (select subdate(curdate(),date_format(curdate(),'%e'))) as ub_end_date, st_id, (select date_format(NOW(), '%Y-%m-%d %H:%i:%s')) as settlement_date, sum(orderamount) settlement_money, count(id) order_num, sum(orderamount) product_money, 0, now() create_date, now() update_date from od_order where create_date >= (SELECT subdate(date_sub(curdate(),interval 1 month), date_format(date_sub(curdate(),interval 1 month),'%e')-1)) and create_date <= (select subdate(curdate(),date_format(curdate(),'%e'))) group by st_id; INSERT INTO uc_bill (ub_number, ub_start_date, ub_end_date, settlement_date, settlement_money, order_num, product_money, settlement_state, create_date, update_date) select (select date_format(curdate(),'%Y%m')-1) as ub_number, (SELECT subdate(date_sub(curdate(),interval 1 month), date_format(date_sub(curdate(),interval 1 month),'%e')-1)) as ub_start_date, (select subdate(curdate(),date_format(curdate(),'%e'))) as ub_end_date, (select date_format(NOW(), '%Y-%m-%d %H:%i:%s')) as settlement_date, sum(orderamount) settlement_money, count(id) order_num, sum(orderamount) product_money, 0, now() create_date, now() update_date from od_order where create_date >= (SELECT subdate(date_sub(curdate(),interval 1 month), date_format(date_sub(curdate(),interval 1 month),'%e')-1)) and create_date <= (select subdate(curdate(),date_format(curdate(),'%e'))) group by DATE_FORMAT(create_date,'%Y-%m'); end ;; DELIMITER ; # 停止任务 ALTER EVENT create_bill DISABLE; #开启任务 ALTER EVENT create_bill enable; # 查看状态 select * from mysql.event select date_format(curdate(),'%e'); # 当月的第几天【几号】 【如:15】 select subdate(curdate(),date_format(curdate(),'%e')); # 前一月最后一天 【如:2015-08-31】 SELECT subdate(date_sub(curdate(),interval 1 month), date_format(date_sub(curdate(),interval 1 month),'%e')-1); #前一月第一天【如:2015-08-01】 select date_format(NOW(), '%Y-%m-%d %H:%i:%s'); #当前时间 select date_sub(now() ,interval -3 day); #当前日期后三天 select date_sub(now() ,interval 3 day); #当前日期前三天 SELECT * FROM company_information WHERE create_date+INTERVAL 12 HOUR<=NOW();#查询12小时之前的数据 delete from company_information where TO_DAYS(NOW())-TO_DAYS(create_date) > 7;#删除7天前的数据