设计初衷,App做前端,用CI+mysql做商城系统和后端,功能设计为限时抢购,app需要定时获取时间进行倒计时。
准备工作:
1.工具Navicat和mysql计划任务功能启用
通过下列语句l爱查询event是否开启
show variables like '%sche%';
show variables like '%sche%';
通过执行下列语句,来开启event_scheduler
set global event_scheduler =1;
set global event_scheduler =1;
1.表结构:
CREATE TABLE `timelimit_product` (
`id` tinyint(8) NOT NULL AUTO_INCREMENT,
`product_id` int(8) NOT NULL COMMENT '商品id',
`start_datetime` datetime NOT NULL COMMENT '开始时间',
`end_datetime` datetime NOT NULL COMMENT '结束时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='限时商品表'
CREATE TABLE `timelimit_product` (
`id` tinyint(8) NOT NULL AUTO_INCREMENT,
`product_id` int(8) NOT NULL COMMENT '商品id',
`start_datetime` datetime NOT NULL COMMENT '开始时间',
`end_datetime` datetime NOT NULL COMMENT '结束时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='限时商品表'
2.Navicat点击 功能 功能 创建新函数 输入名称 timelimit_product
点击 完成
定义输入:
BEGIN
#Routine body goes here...
update timelimit_product set start_datetime=now() where id>0;
END
BEGIN
#Routine body goes here...
update timelimit_product set start_datetime=now() where id>0;
END
保存 测试运行 看看 timelimit_product 表start_datetime字段时间是否更新了
3.点击 Navicat的 事件 功能 创建 事件
定义输入
call timelimit_product();
call timelimit_product();
计划任务 选择 EVERY 2 SECOND
每2秒自动执行一次
保存 为 update
4.这时候计划任务就执行了,每2秒自动执行更新时间任务
功能设计后面需要再添加 自动删除,减少表冗余数据提高性能。
无限星辰 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明使用Navicat创建Mysql倒计时计划任务!