您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)mysql中怎么實(shí)現(xiàn)定時(shí)任務(wù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
定時(shí)任務(wù)
查看event是否開啟: show variables like '%sche%';
將事件計(jì)劃開啟: set global event_scheduler=1;
關(guān)閉事件任務(wù): alter event e_test ON COMPLETION PRESERVE DISABLE;
開戶事件任務(wù): alter event e_test ON COMPLETION PRESERVE ENABLE;
簡(jiǎn)單實(shí)例.
創(chuàng)建表 CREATE TABLE test(endtime DATETIME);
創(chuàng)建存儲(chǔ)過(guò)程test
CREATE PROCEDURE test ()
BEGIN
update examinfo SET endtime = now() WHERE id = 14;
END;
創(chuàng)建event e_test
CREATE EVENT if not exists e_test
on schedule every 30 second
on completion preserve
do call test();
CREATE EVENT if not exists e_test
on schedule every 1 second
on completion preserve
do insert into aa values (now());
每隔30秒將執(zhí)行存儲(chǔ)過(guò)程test,將當(dāng)前時(shí)間更新到examinfo表中id=14的記錄的endtime字段中去.
觸發(fā)器
delimiter //
CREATE TRIGGER trigger_htmlcache BEFORE INSERT ON t_model
FOR EACH ROW BEGIN
if CURDATE()<NEW.time then
INSERT INTO t_htmlcache(id,url) value(NEW.id,NEW.url);
end if;
END;
//
通過(guò)建表->Insert的方式測(cè)試.
DELIMITER $$
DROP PROCEDURE IF EXISTS `njfyedepartment`.`sp_ireport` $$
CREATE ` PROCEDURE `sp_ireport`(IN qureyType VARCHAR(20),IN daytime VARCHAR(20),IN p_ids VARCHAR(50),IN c_ids VARCHAR(50),IN ct1_ids VARCHAR(50),IN ct2_ids VARCHAR(50),IN ku VARCHAR(50),IN ireport_chart varchar(50))
BEGIN
DECLARE i INT DEFAULT 1;
IF qureyType = 'insert' OR qureyType = 'INSERT' THEN
INSERT INTO ireport
(pid,cid,ct1id,ct2id,creatTime,crawlerNumber,WEEK)
SELECT province AS pid,
cityid AS cid,
category1id AS ct1id,
category2id AS ct2id,
(CURRENT_DATE) AS creatTime,COUNT(*) AS crawlerNumber,
(FLOOR(DAYOFMONTH(CURRENT_DATE)/8)+1) AS WEEK
FROM t_model t
WHERE TIME > (CURRENT_DATE-1) AND TIME < (CURRENT_DATE)
AND province IS NOT NULL
AND cityid IS NOT NULL
GROUP BY province,cityid,category1id,category2id;
END IF;
IF qureyType = 'month' OR qureyType = 'MONTH' THEN
IF EXISTS(SELECT * FROM information_schema.`TABLES` T WHERE TABLE_NAME = 'tmp_result' AND TABLE_SCHEMA = ku) THEN
DROP TABLE tmp_result;
END IF;
CREATE TABLE tmp_result
(pid VARCHAR(50),pName VARCHAR(50),cid VARCHAR(50),cName VARCHAR(50),ct1id VARCHAR(50),ct1Name VARCHAR(50),
ct2id VARCHAR(50),ct2Name VARCHAR(50),month2 INTEGER,month3 INTEGER,month4 INTEGER,month5 INTEGER,month6 INTEGER,
month7 INTEGER,month7 INTEGER,month8 INTEGER,month9 INTEGER,month20 INTEGER,month21 INTEGER,month22 INTEGER,heji INTEGER);
lable_exit: BEGIN
SET @SqlCmd = ' INSERT INTO tmp_result (pid,pname,cid,cname,ct1id,ct1name,ct2id,ct2name)
SELECT pid,pname,cid,cname,ct1id,ct1name,ct2id,ct2name FROM
(SELECT ia.pid,a.name AS pname,ia.cid,b.name AS cname,ia.ct1id,c.name AS ct1name,ia.ct2id,d.name AS ct2name
FROM ireport ia
LEFT JOIN province a ON ia.pid=a.id
LEFT JOIN city b ON ia.cid=b.id
LEFT JOIN t_category1 c ON ia.ct1id=c.id
LEFT JOIN t_category2 d ON ia.ct2id=d.id
WHERE YEAR(ia.creatTime)=YEAR(?) ';
IF p_ids IS NOT NULL AND p_ids <> '' THEN
SET @SqlCmd = CONCAT(@SqlCmd , 'and ia.pid in (');
SET @SqlCmd = CONCAT(@SqlCmd, p_ids);
SET @SqlCmd = CONCAT(@SqlCmd, ')');
END IF;
IF c_ids IS NOT NULL AND c_ids <> '' THEN
SET @SqlCmd = CONCAT(@SqlCmd , 'and ia.cid in (');
SET @SqlCmd = CONCAT(@SqlCmd, c_ids);
SET @SqlCmd = CONCAT(@SqlCmd, ')');
END IF;
IF ct1_ids IS NOT NULL AND ct1_ids <> '' THEN
SET @SqlCmd = CONCAT(@SqlCmd , 'and ia.ct1id in (');
SET @SqlCmd = CONCAT(@SqlCmd, ct1_ids);
SET @SqlCmd = CONCAT(@SqlCmd, ')');
END IF;
IF ct2_ids IS NOT NULL AND ct2_ids <> '' THEN
SET @SqlCmd = CONCAT(@SqlCmd , 'and ia.ct2id in (');
SET @SqlCmd = CONCAT(@SqlCmd, ct2_ids);
SET @SqlCmd = CONCAT(@SqlCmd, ')');
END IF;
SET @SqlCmd = CONCAT(@SqlCmd , ') AS ir GROUP BY pid,pname,cid,cname,ct1id,ct1name,ct2id,ct2name;');
PREPARE stmt1 FROM @SqlCmd;
SET @a = daytime;
EXECUTE stmt1 USING @a;
DEALLOCATE PREPARE stmt1;
LEAVE lable_exit;
END lable_exit;
WHILE i <= 12 DO
lable_exit: BEGIN
SET @SqlCmd = 'UPDATE tmp_result AS a,
(
SELECT pid,cid,ct1id,ct2id,SUM(crawlerNumber) AS crawlerNumber FROM
(SELECT pid,cid,ct1id,ct2id,crawlerNumber FROM ireport WHERE MONTH(creatTime)=? AND YEAR(creatTime)=YEAR(?)) AS ir
GROUP BY pid,cid,ct1id,ct2id
) AS b
SET a.month';
SET @SqlCmd = CONCAT(@SqlCmd , i);
SET @SqlCmd = CONCAT(@SqlCmd , '=b.crawlerNumber ');
SET @SqlCmd = CONCAT(@SqlCmd , 'WHERE a.pid=b.pid AND a.cid=b.cid AND a.ct1id=b.ct1id AND a.ct2id=b.ct2id; ');
PREPARE stmt1 FROM @SqlCmd;
SET @a = i;
SET @b = daytime;
EXECUTE stmt1 USING @a,@b;
DEALLOCATE PREPARE stmt1;
LEAVE lable_exit;
END lable_exit;
lable_exit: BEGIN
SET @SqlCmd = 'UPDATE tmp_result SET month';
SET @SqlCmd = CONCAT(@SqlCmd , i);
SET @SqlCmd = CONCAT(@SqlCmd , ' = 0 WHERE month');
SET @SqlCmd = CONCAT(@SqlCmd , i);
SET @SqlCmd = CONCAT(@SqlCmd , ' IS NULL');
PREPARE stmt1 FROM @SqlCmd;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
LEAVE lable_exit;
END lable_exit;
SET i = i + 1;
END WHILE;
UPDATE tmp_result SET heji=month2+month3+month4+month5+month6+month7+month7+month8+month9+month20+month21+month22;
INSERT INTO tmp_result (pid,pname,cid,cname,ct1id,ct1name,ct2id,ct2name,month2,month3,month4,month5,month6,month7,month7,month8,month9,month20,month21,month22,heji)
SELECT '' AS pid,'--' AS pname,'' AS cid,'總合計(jì):' AS cname,'' AS ct1id,'--' AS ct1name,'' AS ct2id,'--' AS ct2name,SUM(month2) AS month2,SUM(month3) AS month3,SUM(month4) AS month4,SUM(month5) AS month5,
SUM(month6) AS month6,SUM(month7) AS month7,SUM(month7) AS month7,SUM(month8) AS month8,SUM(month9) AS month9,SUM(month20) AS month20,SUM(month21) AS month21,SUM(month22) AS month22,SUM(heji) AS heji
FROM tmp_result;
IF ireport_chart = 'report' OR ireport_chart = 'REPORT' THEN
SELECT pid,pName,cid,cName,ct1id,ct1Name,ct2id,ct2Name,month2,month3,month4,month5,month6,month7,month7,month8,month9,month20,month21,month22,heji FROM tmp_result;
END IF;
IF ireport_chart = 'chart' OR ireport_chart = 'CHART' THEN
SELECT '' AS pid,pName,'' AS cid,'' AS cName,'' AS ct1id,'' AS ct1Name,'' AS ct2id,'' AS ct2Name,SUM(month2) as month2,SUM(month3) as month3,SUM(month4) as month4,SUM(month5) as month5,
SUM(month6) as month6,SUM(month7) as month7,SUM(month7) as month7,SUM(month8) as month8,SUM(month9) as month9,SUM(month20) as month20,SUM(month21) as month21,SUM(month22) as month22,'' AS heji
FROM (SELECT pid,pName,cid,cName,ct1id,ct1Name,ct2id,ct2Name,month2,month3,month4,month5,month6,month7,month7,month8,month9,month20,month21,month22 FROM tmp_result WHERE pname <> '--') AS ir
GROUP BY pid;
END IF;
END IF;
IF qureyType = 'week' OR qureyType = 'WEEK' THEN
IF EXISTS(SELECT * FROM information_schema.`TABLES` T WHERE TABLE_NAME = 'tmp_result' AND TABLE_SCHEMA = ku) THEN
DROP TABLE tmp_result;
END IF;
CREATE TABLE tmp_result
(pid VARCHAR(50),pName VARCHAR(50),cid VARCHAR(50),cName VARCHAR(50),ct1id VARCHAR(50),ct1Name VARCHAR(50),ct2id VARCHAR(50),ct2Name VARCHAR(50),week1 INTEGER,week2 INTEGER,week3 INTEGER,week4 INTEGER,heji INTEGER);
lable_exit: BEGIN
SET @SqlCmd = ' INSERT INTO tmp_result (pid,pname,cid,cname,ct1id,ct1name,ct2id,ct2name)
SELECT pid,pname,cid,cname,ct1id,ct1name,ct2id,ct2name FROM
(SELECT ia.pid,a.name AS pname,ia.cid,b.name AS cname,ia.ct1id,c.name AS ct1name,ia.ct2id,d.name AS ct2name
FROM ireport ia
LEFT JOIN province a ON ia.pid=a.id
LEFT JOIN city b ON ia.cid=b.id
LEFT JOIN t_category1 c ON ia.ct1id=c.id
LEFT JOIN t_category2 d ON ia.ct2id=d.id
WHERE YEAR(ia.creatTime)=YEAR(?) And MONTH(ia.creatTime)=MONTH(?) ';
IF p_ids IS NOT NULL AND p_ids <> '' THEN
SET @SqlCmd = CONCAT(@SqlCmd , 'and ia.pid in (');
SET @SqlCmd = CONCAT(@SqlCmd, p_ids);
SET @SqlCmd = CONCAT(@SqlCmd, ')');
END IF;
IF c_ids IS NOT NULL AND c_ids <> '' THEN
SET @SqlCmd = CONCAT(@SqlCmd , 'and ia.cid in (');
SET @SqlCmd = CONCAT(@SqlCmd, c_ids);
SET @SqlCmd = CONCAT(@SqlCmd, ')');
END IF;
IF ct1_ids IS NOT NULL AND ct1_ids <> '' THEN
SET @SqlCmd = CONCAT(@SqlCmd , 'and ia.ct1id in (');
SET @SqlCmd = CONCAT(@SqlCmd, ct1_ids);
SET @SqlCmd = CONCAT(@SqlCmd, ')');
END IF;
IF ct2_ids IS NOT NULL AND ct2_ids <> '' THEN
SET @SqlCmd = CONCAT(@SqlCmd , 'and ia.ct2id in (');
SET @SqlCmd = CONCAT(@SqlCmd, ct2_ids);
SET @SqlCmd = CONCAT(@SqlCmd, ')');
END IF;
SET @SqlCmd = CONCAT(@SqlCmd , ') AS ir GROUP BY pid,pname,cid,cname,ct1id,ct1name,ct2id,ct2name;');
PREPARE stmt1 FROM @SqlCmd;
SET @a = daytime;
EXECUTE stmt1 USING @a,@a;
DEALLOCATE PREPARE stmt1;
LEAVE lable_exit;
END lable_exit;
WHILE i <= 4 DO
lable_exit: BEGIN
SET @SqlCmd = 'UPDATE tmp_result AS a,
(
SELECT pid,cid,ct1id,ct2id,SUM(crawlerNumber) AS crawlerNumber FROM
(SELECT pid,cid,ct1id,ct2id,crawlerNumber FROM ireport WHERE WEEK=? AND MONTH(creatTime)=MONTH(?)) AS ir
GROUP BY pid,cid,ct1id,ct2id
) AS b
SET a.week';
SET @SqlCmd = CONCAT(@SqlCmd , i);
SET @SqlCmd = CONCAT(@SqlCmd , '=b.crawlerNumber ');
SET @SqlCmd = CONCAT(@SqlCmd , 'WHERE a.pid=b.pid AND a.cid=b.cid AND a.ct1id=b.ct1id AND a.ct2id=b.ct2id; ');
PREPARE stmt1 FROM @SqlCmd;
SET @a = i;
SET @b = daytime;
EXECUTE stmt1 USING @a,@b;
DEALLOCATE PREPARE stmt1;
LEAVE lable_exit;
END lable_exit;
lable_exit: BEGIN
SET @SqlCmd = 'UPDATE tmp_result SET week';
SET @SqlCmd = CONCAT(@SqlCmd , i);
SET @SqlCmd = CONCAT(@SqlCmd , ' = 0 WHERE week');
SET @SqlCmd = CONCAT(@SqlCmd , i);
SET @SqlCmd = CONCAT(@SqlCmd , ' IS NULL');
PREPARE stmt1 FROM @SqlCmd;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
LEAVE lable_exit;
END lable_exit;
SET i = i + 1;
END WHILE;
UPDATE tmp_result SET heji=week1+week2+week3+week4;
INSERT INTO tmp_result (pid,pname,cid,cname,ct1id,ct1name,ct2id,ct2name,week1,week2,week3,week4,heji)
SELECT '' AS pid,'--' AS pname,'' AS cid,'總合計(jì):' AS cname,'' AS ct1id,'--' AS ct1name,'' AS ct2id,'--' AS ct2name, SUM(week1) AS week1,SUM(week2) AS week2,SUM(week3) AS week3,SUM(week4) AS week4,SUM(heji) AS heji
FROM tmp_result;
IF ireport_chart = 'report' OR ireport_chart = 'REPORT' THEN
SELECT pid,pName,cid,cName,ct1id,ct1Name,ct2id,ct2Name,week1,week2,week3,week4,heji FROM tmp_result;
END IF;
IF ireport_chart = 'chart' OR ireport_chart = 'CHART' THEN
SELECT '' as pid,pName,'' as cid,'' as cName,'' as ct1id,'' as ct1Name,'' as ct2id,'' as ct2Name,SUM(week1) AS week1,SUM(week2) AS week2,SUM(week3) AS week3,SUM(week4) AS week4,'' as heji
FROM (SELECT pid,cid,pname,cname,week1,week2,week3,week4 FROM tmp_result WHERE pname <> '--') AS ir
GROUP BY pid;
END IF;
END IF;
END $$
DELIMITER ;
看完上述內(nèi)容,你們對(duì)mysql中怎么實(shí)現(xiàn)定時(shí)任務(wù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。