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

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

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

  2. mysql定时器 教程_mysql定时任务

    自 MySQL5.1.6起,增加了一个非常有特色的功能–事件调度器(Event Scheduler),可以用做定时执行某些特定任务(例如:删除记录.对数据进行汇总等等),来取代原先只能由操作系统的计划 ...

  3. mysql 定时计划_MYSQL定时任务

    欢迎大家加入,一起讨论学习 这里我们要完成的定时任务,比较简单 效果:每过X秒 某一些用户积分 加X 第一步:我们先创建一个表和插入数据 /*创建一个表*/ CREATE TABLE`test_nam ...

  4. mysql 定时任务实例_mysql定时任务与存储过程实例

    /** 查看event是否开启 : SHOW VARIABLES LIKE '%event_sche%'; 将事件计划开启 : SET GLOBAL event_scheduler = 1; 将事件计 ...

  5. mysql定时任务 分钟_mysql定时任务

    查看event是否开启 show variables like '%sche%'; 开启event_scheduler set global event_scheduler =1; 创建存储过程spa ...

  6. 编译 php mysql 依赖包_MySQL 5.5.15源码包编译安装

    mysql果然是不愧是目前最火的数据库,自从mysql5.5.8之后,mysql的源码包编译安装都要用到cmake来进行编译了,编译的过程没有本质 mysql果然是不愧是目前最火的数据库,自从mysq ...

  7. mysql 缓存架构_MySQL架构及SQL的执行流程

    1.一条SQL语句的执行流程 1.1通信协议 MySQL 是支持多种通信协议的,可以使用同步/异步的方式,支持长连接/短连接. # 通信类型:同步或者异步 同步通信的特点: 1.同步通信依赖于被调用方 ...

  8. 监控mysql业务数据分析_MySQL数据库监控指标之执行性能总结

    查询性能 MySQL 用户监控查询延迟的方式有很多,既可以通过 MySQL 内置的指标,也可以通过查询性能模式.从 MySQL 5.6.6 版本开始默认启用,MySQL 的 performance_s ...

  9. mysql 优化器_mysql之优化器、执行计划、简单优化

    mysql> explain select * from employees.employees limit 1\G; *************************** 1. row ** ...

最新文章

  1. 电脑电池修复_笔记本电脑不充电是怎么回事?
  2. 【数据结构-图】2.多图详解最小生成树(多图详解+实现代码)
  3. C#使用模板文件批量导出word文档
  4. android多击事件_Android中的多击事件
  5. multiprocessing手记
  6. 专访Two Sigma CEO Nobel: 当前AI投资有太多的炒作
  7. NVIDIA Nsight Eclipse 安装
  8. 表格里加横线一分为二_我告诉你excel表格分割线一分为二
  9. 基于汽车后市场应用 汽车VIN码识别 车架号识别数据SDK
  10. 英国essay与澳洲essay写作区别以及注意事项
  11. 第三只眼网络监控软件简单分析,试用及清除
  12. 【原创】自制操作系统知识储备(一)---流程篇
  13. 解决使用 Bluetooth Audio Receiver 蓝牙传音卡顿问题
  14. 化工原理 --- 流体流动 2
  15. Vue 自定义消息通知组件
  16. Ubuntu使用gzip与bzip2与rar和tar压缩解压
  17. PathInfo模式的支持
  18. windows下实现定时任务重启多台tomcat
  19. 【CSS技巧】文字分散对齐的方法
  20. java二进制视频_Java二进制概念(含视频)

热门文章

  1. 如何使用 SketchUp 最大限度地提高室内设计的效率和创造力?
  2. 什么是股权激励?股权激励的含义
  3. Laravel6.0 + vue.js + elementUI + 微信小程序 实现沃尔玛扫码购
  4. c语言写类似饥荒的游戏,饥荒MOD lua编程0基础入门
  5. 基于SSM的网上宠物店
  6. jira是干什么_JIRA简介
  7. 手机怎样将PDF转为Word可编辑格式,超实用的方法
  8. Verilog中initial和always的执行先后顺序
  9. 谷歌浏览器解决临时跨域问题
  10. 简述UIImagePickerController的属性和用法