mysql 定时任务 每月_mysql 定时任务 每月15号执行
#查看當(dāng)前是否已開(kāi)啟事件調(diào)度器 如果顯示 on 證明已經(jīng)開(kāi)啟 如果顯示off 證明是關(guān)閉狀態(tài)
show variables like 'event_scheduler';
#要想保證能夠執(zhí)行event事件,就必須保證定時(shí)器是開(kāi)啟狀態(tài),默認(rèn)為關(guān)閉狀態(tài)
set global event_scheduler =1;
#或者
set GLOBAL event_scheduler = ON;
# 如果原來(lái)存在該名字的任務(wù)計(jì)劃則先刪除
drop event if exists create_bill;
#每月15號(hào)結(jié)算上個(gè)月的所有訂單 計(jì)算上個(gè)月所有店鋪的統(tǒng)計(jì)數(shù)據(jù) 和 單個(gè)店鋪的數(shù)據(jù)統(tǒng)計(jì)
#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 ;
# 停止任務(wù)
ALTER EVENT create_bill DISABLE;
#開(kāi)啟任務(wù)
ALTER EVENT create_bill enable;
# 查看狀態(tài)
select * from mysql.event
select date_format(curdate(),'%e');? ? # 當(dāng)月的第幾天【幾號(hào)】? 【如: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');? #當(dāng)前時(shí)間
select date_sub(now() ,interval -3 day); #當(dāng)前日期后三天
select date_sub(now() ,interval 3 day); #當(dāng)前日期前三天
SELECT * FROM company_information WHERE create_date+INTERVAL 12 HOUR<=NOW();#查詢12小時(shí)之前的數(shù)據(jù)
delete from company_information where TO_DAYS(NOW())-TO_DAYS(create_date) > 7;#刪除7天前的數(shù)據(jù)
第一次寫(xiě)定時(shí)任務(wù)? 感覺(jué)良好 特此記錄 !!!
總結(jié)
以上是生活随笔為你收集整理的mysql 定时任务 每月_mysql 定时任务 每月15号执行的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 辐射发射测试软件,如何进行电子设备的辐射
- 下一篇: YesPlayMusic for Mac