#檢視當前是否已開啟事件排程器 如果顯示 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天前的資料
第一次寫定時任務 感覺良好 特此記錄 !!!
mysql 定時任務 生效 mysql定時任務
1.檢視是否開啟定時策略 show variables like event sche 若出現如下圖,則此時是關閉狀態 開啟定時策略 重啟無效 set global event scheduler 1 則需要在配置檔案my.ini的設定 重啟有效 mysqld event scheduler on ...
mysql 定時任務
檢視event是否開啟 複製 如下 show variables like sche 將事件計畫開啟 複製 如下 set global event scheduler 1 建立儲存過程test 複製 如下 create procedure test begin update examinfo set...
mysql定時任務
show variables like event scheduler 開啟定時任務 set global event scheduler on 定時更新 on completion not preserve 表示當事件不會再發生的情況下,刪除事件 注意特定時間執行的事件,如果設定了該引數,執行完畢...