mysql定时任务

1.语法讲解

SET GLOBAL event_scheduler = 1;


SET GLOBAL event_scheduler = ON;
来开启,也可以直接在启动命令加上“–event_scheduler=1”,例如:
mysqld ... --event_scheduler=1
my.ini or my.cnf 中的
[mysqld]
添加 event_scheduler=ON
创建事件(CREATE EVENT)
先来看一下它的语法:
CREATE EVENT [IF NOT EXISTS] event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE]
[COMMENT 'comment']
DO sql_statement;
schedule:
AT TIMESTAMP [+ INTERVAL INTERVAL]
| EVERY INTERVAL [STARTS TIMESTAMP] [ENDS TIMESTAMP]
INTERVAL:
quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |

            WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |

 DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}

2.实际演示

实例1:

public function addorder() {
		$data ['order_sn'] = '5555588888';
		$data ['user_id'] = 5555;
		$data ['status'] = 0;
		$data ['goods_id'] = 1555;
		$data ['sku_id'] = 0;
		$data ['price'] = 50;
		$re = M ( 'big.ih_order' )->add ( $data );
		if ($re) {
			$sql = "CREATE EVENT order_".$re."
                    ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE 
                    ON COMPLETION NOT PRESERVE
                    DO 
                    begin
                    update big.ih_order set status=1 where id=".$re.";
                    update big.ih_store set number=number+1 where id=1;
                    end
                    ";
			$res=M()->execute($sql);
		}
		echo $re;
		echo '<br/>';
		echo $res;
	}
实例2

public function addorderevent($uid=21,$oid=602){
		$otime = M('chaindb.user_set')->field('otime')->where("uid={$uid}")->find();
	    if(!empty($otime)&&$otime['otime']>0){
			/*/*查询已经过期订单,并还原对应库存
				*/
	    	$sql = "CREATE EVENT order_".$oid."
                    ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL ".$otime['otime']." SECOND 
                    ON COMPLETION NOT PRESERVE
                    DO 
                    begin
                    update chaindb.cus_order as co, chaindb.user_goods as g,chaindb.cus_order_detail as o set g.inventory=g.inventory+o.quantity,g.sales=g.sales-o.quantity,co.ostatus=0 where co.oid=".$oid." and co.ostatus=1 and co.pstatus=0 and (co.paytype=0 or co.paytype>1) and co.oid=o.oid and o.oid=".$oid."  and g.gid=o.gid and o.aid=0;
                    update chaindb.cus_order as co, chaindb.user_goods as g,chaindb.cus_order_detail as o,chaindb.user_goods_attr as a set a.inventory=a.inventory+o.quantity,g.sales=g.sales-o.quantity,co.ostatus=0 where co.oid=".$oid." and co.ostatus=1 and co.pstatus=0 and (co.paytype=0 or co.paytype>1) and co.oid=o.oid and o.oid=".$oid."  and g.gid=o.gid and o.aid>0 and o.aid=a.aid;
                    end
                    ";
		
			$res=M()->execute($sql);
		}
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值