简体   繁体   中英

MySQL Trigger for aggregating one table's rows to another table

I have 2 MySQL Tables, one that will be updated with new rows that needs to be aggregated(TABLE 1). and one that should be automatically filled with aggregated data using a mysql trigger (TABLE 2).

Given the tables schema:

Example for TABLE 1: Table to be aggregated 在此输入图像描述

UNIQUE KEY uniqueid ( uniqueid , ci , ai , status )

Example for TABLE 2: The aggregated table 在此输入图像描述

UNIQUE KEY IX_Unique_daily ( ai , ci , day )

The SQL Trigger should be something like:

DELIMITER $$

CREATE TRIGGER yii_botclient2 . st_unqiue_trigger AFTER INSERT ON yii_botclient2 . st_unique FOR EACH ROW BEGIN

SET @myDay := DATE(NEW.date);
SET @today = DATE();
SET isDaily := SELECT COUNT(id) FROM `yii_botclient2`.`st_daily` WHERE `ai` = NEW.ai AND st_daily.ci = NEW.ci AND st_daily.day = @myDay;
SET myDaily = IFNULL(isDaily, 'No');

IF (myDaily = 'No') THEN
    IF NEW.status = 0
        INSERT INTO st_daily (`day`, `ai`,`ci`,`impressions`,`updated`) VALUES (@today,NEW.ai,NEW.ci,1,NOW());
    ELSEIF NEW.status = 1 
        INSERT INTO st_daily (`day`, `ai`,`ci`,`clicks`,`updated`) VALUES (@today,NEW.ai,NEW.ci,1,NOW());
    ELSEIF NEW.status = 2
        INSERT INTO st_daily (`day`, `ai`,`ci`,`leads`,`updated`) VALUES (@today,NEW.ai,NEW.ci,1,NOW());
    ELSEIF NEW.status = 3 
        INSERT INTO st_daily (`day`, `ai`,`ci`,`ftds`,`updated`) VALUES (@today,NEW.ai,NEW.ci,1,NOW());
    END IF;
ELSE
    IF NEW.status = 0
        UPDATE st_daily SET `impressions` = `impressions` + 1 WHERE st_daily.ci = NEW.ci AND st_daily.ai = NEW.ai AND st_daily.day = @myDay;
    ELSEIF NEW.status = 1 
        UPDATE st_daily SET `clicks` = `clicks` + 1 WHERE st_daily.ci = NEW.ci AND st_daily.ai = NEW.ai AND st_daily.day = @myDay;
    ELSEIF NEW.status = 2
        UPDATE st_daily SET `leads` = `leads` + 1 WHERE st_daily.ci = NEW.ci AND st_daily.ai = NEW.ai AND st_daily.day = @myDay;
    ELSEIF NEW.status = 3 
        UPDATE st_daily SET `ftds` = `ftds` + 1 WHERE st_daily.ci = NEW.ci AND st_daily.ai = NEW.ai AND st_daily.day = @myDay;
    END IF;
END IF;

END$$

DELIMITER ;

Can you please help with the MySQL Trigger ?

Declare your variables, use = in set statements, Date functions need a parameter, if statements need then's Something like this compiles to the point where it complains about tables not existing, If you want any further help please provide the tables in the question as text or a sql fiddle.

drop trigger if exists st_unqiue_trigger; 

DELIMITER $$

CREATE TRIGGER yii_botclient2.st_unqiue_trigger AFTER INSERT ON st_unique FOR EACH ROW BEGIN
declare myday date;
declare today  date;
declare isdaily int;
declare mydaily varchar(2);
SET myDay = DATE(NEW.date);
SET today = date(now());
SELECT COUNT(id) into isdaily FROM `yii_botclient2`.`st_daily` WHERE `ai` = NEW.ai AND st_daily.ci = NEW.ci AND st_daily.day = myDay;
SET myDaily = IFNULL(isDaily, 'No');

IF (myDaily = 'No') THEN
    IF NEW.status = 0 then
        INSERT INTO st_daily (`day`, `ai`,`ci`,`impressions`,`updated`) VALUES (today,NEW.ai,NEW.ci,1,NOW());
    ELSEIF NEW.status = 1 then
        INSERT INTO st_daily (`day`, `ai`,`ci`,`clicks`,`updated`) VALUES (today,NEW.ai,NEW.ci,1,NOW());
    ELSEIF NEW.status = 2 then
        INSERT INTO st_daily (`day`, `ai`,`ci`,`leads`,`updated`) VALUES (today,NEW.ai,NEW.ci,1,NOW());
    ELSEIF NEW.status = 3 then
        INSERT INTO st_daily (`day`, `ai`,`ci`,`ftds`,`updated`) VALUES (today,NEW.ai,NEW.ci,1,NOW());
    END IF;
ELSE
    IF NEW.status = 0 then
        UPDATE st_daily SET `impressions` = `impressions` + 1 WHERE st_daily.ci = NEW.ci AND st_daily.ai = NEW.ai AND st_daily.day = myDay;
    ELSEIF NEW.status = 1 then
        UPDATE st_daily SET `clicks` = `clicks` + 1 WHERE st_daily.ci = NEW.ci AND st_daily.ai = NEW.ai AND st_daily.day = myDay;
    ELSEIF NEW.status = 2 then
        UPDATE st_daily SET `leads` = `leads` + 1 WHERE st_daily.ci = NEW.ci AND st_daily.ai = NEW.ai AND st_daily.day = myDay;
    ELSEIF NEW.status = 3 then
        UPDATE st_daily SET `ftds` = `ftds` + 1 WHERE st_daily.ci = NEW.ci AND st_daily.ai = NEW.ai AND st_daily.day = myDay;
    END IF;
END IF;

END $$

delimiter ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM