ERROR #1054 - Unknown column 'program_id' in 'NEW

一笑奈何 提交于 2020-01-16 08:50:43

问题


Tring to achieve-

I am trying to update color_status in tb_sites_3 (3 will be dynamic based on program_id which we will get from tb_tickets) Whenever any insert is made on tb_jobs.

ERROR

While creating a trigger I am getting the following error ERROR #1054 - Unknown column 'program_id' in 'NEW'

tb_tickets

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 ;

Table definations

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

回答1:


CREATE TRIGGER trig_job_color AFTER INSERT ON tb_jobs FOR EACH ROW BEGIN SET @program_id = (Select program_id from tb_tickets where 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



回答2:


This worked for me using variables.

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 ;


来源:https://stackoverflow.com/questions/56964816/error-1054-unknown-column-program-id-in-new

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!