【问题标题】:ERROR #1054 - Unknown column 'program_id' in 'NEW错误 #1054 - 'NEW' 中的未知列 'program_id'
【发布时间】:2019-11-19 17:36:09
【问题描述】:

努力实现-

我正在尝试更新 tb_sites_3 中的 color_status(3 将基于我们将从 tb_tickets 获得的 program_id 动态)每当在 tb_jobs 上进行任何插入时。

错误

创建触发器时出现以下错误 错误 #1054 - 'NEW' 中的未知列 'program_id'

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 ;

表格定义

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

【问题讨论】:

  • 触发器在 tb_jobs 上,非常新。值仅引用该表。
  • 但要获得 program_id,我必须将 tb_jobs 和 tb_tickets 作为 program_id 加入,状态包含在 tb_tickets @P.Salmon
  • 也许你应该使用声明的变量?不要忘记我所看到的只是无效的代码,您还没有描述您要实现的目标。您还应该将表定义、示例数据、插入语句和预期结果添加为文本。
  • 如果在 tb_jobs 中进行了任何插入,我想更新 tb_sites_3 中的 color_status。 @P.鲑鱼
  • 您能否在 tb.jobs 中为 job_id 1 多次插入(例如)以及没有 order by 的限制是什么(修辞问题 - 非常没用)

标签: mysql sql triggers


【解决方案1】:

插入后创建触发器trig_job_colortb_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

【讨论】:

    【解决方案2】:

    这对我使用变量很有用。

    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 ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-01
      • 2012-03-09
      • 2017-03-17
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      相关资源
      最近更新 更多