日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql 定时任务 每月_mysql 定时任务 每月15号执行

發布時間:2023/12/31 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 定时任务 每月_mysql 定时任务 每月15号执行 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#查看當前是否已開啟事件調度器 如果顯示 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 定时任务 每月15号执行的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。