#查看当前是否已开启事件调度器 如果显示 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 定时任务 每月15号执行相关推荐

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

    #查看当前是否已开启事件调度器 如果显示 on 证明已经开启 如果显示off 证明是关闭状态 show variables like 'event_scheduler'; #要想保证能够执行event ...

  2. java 定时任务每月1号发送excel邮件

    java 定时任务每月1号发送excel邮件 1.生成excel 数据流 public ByteArrayInputStream exportxls(){String preDate = " ...

  3. 支付宝花呗还款日怎么从每9号调整支付宝花呗还款日为每月15号或每月20号还款?

    支付宝花呗还款日怎么从每9号调整支付宝花呗还款日为每月15号或每月20号还款? 1.打开支付宝,在底部点击[我的]: 2.在支付宝我的里找到花呗,并点击进入: 3.在支付宝花呗的右上角找到[设置]并点 ...

  4. python定时任务每月1号_Python 定时任务框架 APScheduler 详解

    APScheduler 最近想写个任务调度程序,于是研究了下 Python 中的任务调度工具,比较有名的是:Celery,RQ,APScheduler. Celery:非常强大的分布式任务调度框架 R ...

  5. mysql定时任务,每天定时执行

    最近在公司需要做一个mysql每天定时执行存储过程,完成后记录一下,以便日后查阅. 这里我们实现一个定时任务,每天凌晨1点执行一个存储过程,向用户表插入一条记录. 首先查看mysql数据库是否开启了事 ...

  6. 满满干货!mysql定时任务每天固定时间执行

    Spring 全家桶: Spring 原理 Spring面试题 思维导图 面试题 Spring视频 Spring 原理 Spring特点 Spring 核心组件 Spring常用模块 Spring主要 ...

  7. linux cron记录时间,crontab每分钟,5,10分钟,每小时,每天零点,每周日0点,每月1号0点,每年1月1日执行脚本,linux shell定时任务...

    crontab每分钟,5,10分钟,每小时,每天零点,每周日0点,每月1号0点,每年1月1日执行脚本,linux shell定时任务 在linux系统里做维护和开发时, 经常会遇到要用crontabl ...

  8. python定时任务,隔月执行,隔定时执行

    #BlockingScheduler定时任务 from apscheduler.schedulers.blocking import BlockingScheduler from datetime i ...

  9. PHP获取一段时间内的每个周几, 每月几号, 遇到特殊日子就往后延

    2016年11月1日 16:18:19 星期二 主要用到的函数是 strtotime() strtotime('+1 Tuesday', $timestamp) 获取下周二, 从时间戳$timesta ...

最新文章

  1. 自己拿项目,软件设计开发,释放你的力量
  2. LINUX下忘记MySQL的ROOT密码后修改,以及添加访问IP。
  3. 数据中心停机事故的教训:关注基础设施
  4. 全站仪数据导入电脑_三鼎762R系列全站仪的SD卡传输教程
  5. JZOJ 5407. 【NOIP2017提高A组集训10.21】Deep
  6. mysql数据库安全配置文件_MySQL数据库安全配置
  7. VirtualBox虚拟机网络环境解析和搭建-NAT、桥接、Host-Only、Internal、端口映射
  8. 前端学习(285):移动端简单知识
  9. java解析上的jar包里的pom_Maven引入本地Jar包并打包进War包中的方法
  10. matlab话pca的双标图biplot,r – 用ggplot2绘制pca biplot
  11. 罗森伯格高端布线助力昆山基地
  12. telnet不通linux下tomcat端口
  13. 北大才女文章 《卖米》感动人心
  14. matlab 矩阵分行标准化,matlab矩阵标准化
  15. 轻运维|无人干预,易捷行云新一代私有云一键式扩容
  16. 新版WIFI小程序分销系统微信源码序WiFi大师版流量主搭建独立源码WiFi分销源码
  17. Skywalking全部
  18. 微信小程序对接快递鸟接口返回格式有误的解决
  19. 为什么建议大家使用 Linux 开发?爽++
  20. 51信用卡股价年初至今上浮5倍,引入银行背景高管担任行政总裁

热门文章

  1. 叠氮试剂79598-53-1,6-Azidohexanoic Acid,6-叠氮基己酸,末端羧酸可与伯胺基反应
  2. Photoshop修图的常用方法与技巧一
  3. Python3.7中,Django配置MySql数据库
  4. Python制作微信自动回复机器人,打游戏时自动回复女朋友消息
  5. 二叉树交换左右子树的递归与非递归算法
  6. 如何批量修改文件名、照片文件名
  7. mysql 不会丢失数据吗_讨论MySQL丢失数据的几种情况
  8. 状态栏和导航栏重叠,解决办法
  9. 卸载SQL server后,本地文件无法删除
  10. 判断7张扑克牌是否含有同花顺(5张同花顺子)