正在努力实现-
我正在尝试更新tb_sites_3中的color_status(3将根据从tb_tickets获得的program_id是动态的),只要在tb_jobs上进行任何插入。
错误
在创建触发器时,出现以下错误 错误#1054-“新建”中的未知列“ program_id”
tb_jobs
tb_sites_3
DELIMITER //
CREATE TRIGGER trig_job_color
BEFORE INSERT ON `tb_jobs`
FOR EACH ROW
BEGIN
SET NEW.program_id = (Select program_id from tb_tickets
where tb_tickets.job_id = NEW.job_id);
SET NEW.status = (Select status from tb_tickets
where tb_tickets.job_id = NEW.job_id);
CASE NEW.program_id
WHEN 1 THEN
UPDATE tb_sites_1
SET color_status = NEW.status
WHERE site_id = NEW.site_id;
WHEN 2 THEN
UPDATE tb_sites_2
SET color_status = NEW.status
WHERE site_id = NEW.site_id;
WHEN 3 THEN
UPDATE tb_sites_3
SET color_status = NEW.status
WHERE site_id = NEW.site_id;
END CASE;
END //
DELIMITER ;
表格定义
tb_tickets
CREATE TABLE `tb_tickets` (
`id` int(15) NOT NULL,
`ticket_id` int(15) NOT NULL,
`job_id` int(11) NOT NULL,
`site_id` varchar(200) NOT NULL,
`program_id` int(11) NOT NULL,
`status` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
tb_jobs
CREATE TABLE `tb_jobs` (
`job_id` int(11) NOT NULL AUTO_INCREMENT,
`job_creation` date DEFAULT NULL,
PRIMARY KEY (`job_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
tb_sites_3
CREATE TABLE `tb_sites_3` (
`id` int(15) NOT NULL AUTO_INCREMENT,
`color_status` int(15) NOT NULL,
`site_id` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
答案 0 :(得分:1)
在插入trig_job_color
之后创建触发器tb_jobs
对于每个行开始
SET @program_id =(从tb_tickets中选择program_id
其中tb_tickets.job_id = NEW.job_id);
SET @newstatus = (Select status from tb_tickets
where tb_tickets.job_id = NEW.job_id);
SET @newsite_id = (Select site_id from tb_tickets
where tb_tickets.job_id = NEW.job_id);
CASE @program_id
WHEN 1 THEN
UPDATE tb_sites_3
SET tb_sites_3.color_status = @newstatus
WHERE tb_sites_3.site_id = @newsite_id;
WHEN 2 THEN
UPDATE tb_sites_3
SET tb_sites_3.color_status = @newstatus
WHERE tb_sites_3.site_id = @newsite_id;
WHEN 3 THEN
UPDATE tb_sites_3
SET tb_sites_3.color_status = @newstatus
WHERE tb_sites_3.site_id = @newsite_id;
END CASE;
END
答案 1 :(得分:0)
这对我来说使用变量很有效。
DELIMITER //
CREATE TRIGGER trig_job_color
AFTER INSERT ON `tb_jobs`
FOR EACH ROW
BEGIN
DECLARE x, y INT DEFAULT 0;
DECLARE z varchar(50);
SET x = (Select program_id from tb_tickets
where tb_tickets.job_id = NEW.job_id);
SET y = (Select status from tb_tickets
where tb_tickets.job_id = NEW.job_id);
SET Z = (Select site_id from tb_tickets
where tb_tickets.job_id = NEW.job_id);
CASE x
WHEN 1 THEN
UPDATE tb_sites_1
SET color_status = y
WHERE site_id = z;
WHEN 2 THEN
UPDATE tb_sites_2
SET color_status = y
WHERE site_id = z;
WHEN 3 THEN
UPDATE tb_sites_3
SET color_status = y
WHERE site_id = z;
END CASE;
END //
DELIMITER ;