MySQL触发器,用于将一个表的行聚合到另一个表

时间:2017-02-07 09:33:06

标签: mysql sql triggers aggregation

我有2个MySQL表, 将使用需要聚合的新行更新的一个(表1)。 并且应该使用mysql触发器自动填充聚合数据(表2)。

给定表模式:

表1的示例: 要汇总的表 enter image description here

独特的钥匙uniqueiduniqueidciaistatus

表2的示例: 汇总表 enter image description here

UNIQUE KEY IX_Unique_dailyaiciday

SQL触发器应该类似于:

DELIMITER $$

CREATE     TRIGGER yii_botclient2st_unqiue_trigger插入后     在yii_botclient2st_unique     每行开始

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;

你能帮忙解决MySQL触发器问题吗?

1 个答案:

答案 0 :(得分:0)

声明你的变量,在set语句中使用=,Date函数需要一个参数,if语句是否需要 这样的东西编译到它抱怨表不存在的地步,如果你想要任何进一步的帮助,请提供问题中的表格作为文本或sql小提琴。

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 ;