bitsCN.com
定时任务
查看event是否开启: show variables like '%sche%';
将事件计划开启: set global event_scheduler=1;
关闭事件任务: alter event e_test ON COMPLETION PRESERVE DISABLE;
开户事件任务: alter event e_test ON COMPLETION PRESERVE ENABLE;
简单实例.
创建表 CREATE TABLE test(endtime DATETIME);
创建存储过程test
CREATE PROCEDURE test ()
BEGIN
update examinfo SET endtime = now() WHERE id = 14;
END;
创建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秒将执行存储过程test,将当前时间更新到examinfo表中id=14的记录的endtime字段中去.
触发器
delimiter //
CREATE TRIGGER trigger_htmlcache BEFORE INSERT ON t_model
FOR EACH ROW BEGIN
if CURDATE()
end if;
END;
//
通过建表->Insert的方式测试.
DELIMITER $$
DROP PROCEDURE IF EXISTS `njfyedepartment`.`sp_ireport` $$
CREATE DEFINER=`root`@`%` 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),month1 INTEGER,month2 INTEGER,month3 INTEGER,month4 INTEGER,month5 INTEGER,
month6 INTEGER,month7 INTEGER,month8 INTEGER,month9 INTEGER,month10 INTEGER,month11 INTEGER,month12 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 省份 a ON ia.pid=a.id
LEFT JOIN 城市 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 NOT NULL AND p_ids <> > SET @SqlCmd = CONCAT(@SqlCmd , 'and ia.pid in (');
SET @SqlCmd = CONCAT(@SqlCmd, p_ids);
SET @SqlCmd = CONCAT(@SqlCmd, ')' );
END IF;
IF c_ids 不为空且 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 不为空并且 ct1_ids ' ' THEN
SET @SqlCmd = CONCAT(@SqlCmd , 'and ia.ct1id in (');
SET @SqlCmd = CONCAT(@SqlCmd, ct1_ids);
SET @SqlCmd = CONCAT(@SqlCmd , ')');
END IF;
如果 ct2_ids 不为空且 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;');
从 @SqlCmd 准备 stmt1;
SET @a = daytime;
使用 @a 执行 stmt1;
解除分配准备 stmt1;
离开 lable_exit;
END lable_exit;
SELECT '' AS pid,'--' AS pname,'' AS cid,'总合计:' AS cname,'' AS ct1id, '--' AS ct1name,'' AS ct2id,'--' AS ct2name,SUM(月1) AS 月1,SUM(月2) AS 月2,SUM(月3) AS 月3,SUM(月4) AS 月4,
SUM(5 月) AS 5 月,SUM(6 月) AS 6 月,SUM(7 月) AS 7 月,SUM(8 月) AS 8 月,SUM(9 月) AS 9 月,SUM(10 月) AS 10 月,SUM(11 月) AS 11 月,SUM (月 12) AS 月 12,SUM(heji) AS heji
FROM tmp_result;
IF ireport_chart = 'report' OR ireport_chart = 'REPORT' THEN
选择 pid,pName,cid,cName,ct1id,ct1Name,ct2id,ct2Name,month1,month2,month3,month4,month5,month6,month7,月 8,月 9,月 10,月 11,月 12,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(月1)为月1,SUM(月2)为月2,SUM(月3)为月3,SUM(月 4) 为月 4,
SUM(月 5) 为月 5,SUM(月 6) 为月 6,SUM(月 7) 为月 7,SUM(月 8) 为月 8,SUM(月 9) 为月 9,SUM(月 10) 为月 10, SUM(month11) asmonth11,SUM(month12)asmonth12,'' AS heji
FROM (SELECT pid,pName,cid,cName,ct1id,ct1Name,ct2id,ct2Name,month1,month2,month3,month4,month5,月 6,月 7,月 8,月 9,月 10,月 11,月 12 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
删除表 tmp_result;
END IF;
创建表 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)
选择 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 省份 a ON ia.pid=a.id
LEFT连接城市 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(?) 和 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 不为空且 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 不为空且 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 不为空且 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;');
从 @SqlCmd 准备 stmt1;
SET @a = daytime;
使用 @a,@a 执行 stmt1;
解除分配准备 stmt1;
离开 lable_exit;
END lable_exit;
WHILE i lable_exit: BEGIN
SET @SqlCmd = 'UPDATE tmp_result AS a,
(
SELECT pid,cid,ct1id,ct2id,SUM(crawlerNumber ) AS scraperNumber 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 , '其中 a.pid=b.pid AND a.cid=b.cid AND a.ct1id=b.ct1id AND a.ct2id=b.ct2id; ');
从 @SqlCmd 准备 stmt1;
SET @a = i;
SET @b = daytime;
使用 @a,@b 执行 stmt1;
解除分配准备 stmt1;
离开 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');
准备 stmt1 FROM @SqlCmd;
EXECUTE stmt1;
取消分配准备 stmt1;
离开 lable_exit;
END lable_exit;
SET i = i 1;
END WHILE;
UPDATE tmp_result SET heji =周1周2周3周4;
INSERT INTO tmp_result (pid,pname,cid,cname,ct1id,ct1name,ct2id,ct2name,week1,week2,week3,week4,heji)
SELECT '' AS pid,'--' AS 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,SUM(heji) AS heji
FROM tmp_result;
IF ireport_chart = 'report' OR ireport_chart = 'REPORT' THEN
选择 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
选择'' 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 ;
作者“李丽芬的博客”