Reputation: 11
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 ?
Upvotes: 1
Views: 742
Reputation: 17655
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 ;
Upvotes: 0