用于将一个表的行聚合到另一个表的 MySQL 触发器
Posted
技术标签:
【中文标题】用于将一个表的行聚合到另一个表的 MySQL 触发器【英文标题】:MySQL Trigger for aggregating one table's rows to another table 【发布时间】:2017-06-24 11:37:17 【问题描述】:我有 2 个 mysql 表, 一个将使用需要聚合的新行进行更新(表 1)。 还有一个应该使用 mysql 触发器自动填充聚合数据(表 2)。
给定表格架构:
表 1 的示例: 要聚合的表
唯一键uniqueid
(uniqueid
,ci
,ai
,status
)
表 2 的示例: 汇总表
唯一键IX_Unique_daily
(ai
,ci
,day
)
SQL 触发器应该是这样的:
分隔符 $$
创建
插入后触发yii_botclient2
.st_unqiue_trigger
开yii_botclient2
.st_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$$
分隔符;
您能帮忙解决一下 MySQL 触发器吗?
【问题讨论】:
你有什么问题? 我无法执行触发器查询,我不明白它有什么问题 【参考方案1】:声明变量,在 set 语句中使用 =,Date 函数需要一个参数,if 语句需要 then 这样的东西编译到它抱怨表不存在的地步,如果您需要任何进一步的帮助,请将问题中的表作为文本或 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 ;
【讨论】:
以上是关于用于将一个表的行聚合到另一个表的 MySQL 触发器的主要内容,如果未能解决你的问题,请参考以下文章