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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

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

發(fā)布時(shí)間:2023/12/31 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 定时任务 每月_mysql 定时任务 每月15号执行 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

#查看當(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。