1)函数
BEGIN
# 给每个类目VIP购买数量随机增加1-3
DECLARE done INT DEFAULT 0 ; -- 游标开关
DECLARE randNum INT DEFAULT 0 ; -- 随机数
DECLARE sellCount INT DEFAULT 0 ; -- 购买数量
DECLARE groupBuyId INT default 0; --
DECLARE groupBuying CURSOR FOR SELECT t.id FROM tableName t WHERE t.group_type = 4 AND t.`status` = 1 AND NOW() >= t.start_time AND NOW() <= t.end_time;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1 ;
OPEN groupBuying ;
read_loop :
LOOP
FETCH groupBuying INTO groupBuyId ;
IF done = 1
THEN LEAVE read_loop ;
END IF ;
SELECT t.sell_count INTO sellCount FROM tableName t WHERE t.id = groupBuyId;
set randNum = ceiling(rand()* 3) + sellCount;
UPDATE tableName b SET b.sell_count = randNum WHERE b.id = groupBuyId;
END LOOP ;
CLOSE groupBuying ;
RETURN 1;
END
2)定时器