Mysql scheduled task _ MySQL

Source: Internet
Author: User
Mysql scheduled task bitsCN.com

Scheduled task
Check whether the event is enabled: show variables like '% sche % ';
Enable the event plan: set global event_scheduler = 1;
Close event task: alter event e_test on completion preserve disable;
Account Opening event task: alter event e_test on completion preserve enable;

Simple instance.
Create table test (endtime DATETIME );

Create a stored procedure test
Create procedure test ()
BEGIN
Update examinfo SET endtime = now () WHERE id = 14;
END;

Create 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 ());
The stored procedure test is executed every 30 seconds, and the current time is updated to the endtime field of the record id = 14 in examinfo.

Trigger
Delimiter //
Create trigger trigger_htmlcache before insert on t_model
FOR EACH ROW BEGIN
If CURDATE () Insert into t_htmlcache (id, url) value (NEW. id, NEW. url );
End if;
END;
//
Test by creating a table-> Insert.

DELIMITER $
Drop procedure if exists 'njfyedepartment '. 'sp _ port port' $
Create definer = 'root' @ '%' PROCEDURE 'sp _ Port '(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, crawler number, WEEK)
SELECT province AS pid,
Cityid AS cid,
Category1id AS ct1id,
Category2id AS ct2id,
(CURRENT_DATE) AS creatTime, COUNT (*) AS crawler number,
(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, mon2integer, 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,. 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 @;
Deallocate prepare stmt1;
LEAVE lable_exit;
END lable_exit;

WHILE I <= 12 DO
Lable_exit: BEGIN
SET @ SqlCmd = 'update tmp_result AS,
(
SELECT pid, cid, ct1id, ct2id, SUM (crawler number) AS crawler number FROM
(SELECT pid, cid, ct1id, ct2id, crawler number 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. pid = B. pid AND. cid = B. cid AND. ct1id = B. ct1id AND. 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 = month1 + month1 + month3 + month4 + month5 + month6 + month7 + month8 + month9 + month10 + month11 + month12;

Insert into tmp_result (pid, pname, cid, cname, ct1id, ct1name, region, ct2name, month1, month3, month4, month5, month6, month7, month8, month9, month10, month11, month12, heji)
SELECT ''as pid, '-- 'As pname, ''As cid,' total: 'As cname, ''As ct1id, '-- 'As ct1name, ''As ct2id, '--' AS ct2name, SUM (month1) AS month1, SUM (month1) AS mon22. SUM (month3) AS month3, SUM (month4) AS month4,
SUM (month5) AS month5, SUM (month6) AS month6, SUM (month7) AS month7, SUM (month8) AS month8, SUM (month9) AS month9, SUM (month10) AS month10, SUM (month11) AS month11, SUM (month12) AS month12, 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, month1, month3, month4, month5, month6, month7, month8, month9, month10, month11, month12, 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 (month1) as month1, SUM (month1) as mon22. SUM (month3) as month3, SUM (month4) as month4,
SUM (month5) as month5, SUM (month6) as month6, SUM (month7) as month7, SUM (month8) as month8, SUM (month9) as month9, SUM (month10) as month10, SUM (month11) as month11, SUM (month12) as month12, ''AS heji
FROM (SELECT pid, pName, cid, cName, ct1id, ct1Name, ct2id, ct2Name, month1, month3, month4, month5, month6, month7, month8, month9, month10, month11, month12 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,. 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, @;
Deallocate prepare stmt1;
LEAVE lable_exit;
END lable_exit;

WHILE I <= 4 DO
Lable_exit: BEGIN
SET @ SqlCmd = 'update tmp_result AS,
(
SELECT pid, cid, ct1id, ct2id, SUM (crawler number) AS crawler number FROM
(SELECT pid, cid, ct1id, ct2id, crawler number 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. pid = B. pid AND. cid = B. cid AND. ct1id = B. ct1id AND. 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,' total: '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;

Author: "Li Lifen's blog"

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.