MySQL触发器循环查询结果中的多行数据

10

大家好,我有一个包含许多表和外键的数据库,就像这样:

CREATE TABLE IF NOT EXISTS `articulos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nombre` varchar(63) NOT NULL,
  `contenido` text NOT NULL,
  `normas_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=138 ;

CREATE TABLE IF NOT EXISTS `aspectosambientales` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nombre` varchar(63) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=28 ;

CREATE TABLE IF NOT EXISTS `aspectosambientales_articulos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `aspectosambientales_id` int(11) NOT NULL,
  `articulos_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_aspaspectosambientales1`      (`aspectosambientales_id`),
  KEY `fk_aspee` (`articulos_id`) 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 UTO_INCREMENT=225 ;

CREATE TABLE IF NOT EXISTS `empresas` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `razonsocial` varchar(127) DEFAULT NULL,
  `nit` varchar(63) DEFAULT NULL,
  `direccion` varchar(127) DEFAULT NULL,
  `telefono` varchar(15) DEFAULT NULL,
  `web` varchar(63) DEFAULT NULL,
  `auth_user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

CREATE TABLE IF NOT EXISTS `articulos_empresas` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `empresas_id` int(11) NOT NULL,
  `articulo_id` int(11) NOT NULL,
  `acciones` text,
  `responsable` varchar(255) DEFAULT NULL,
  `plazo` date DEFAULT NULL,
  `cumplido` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_normas_empresas_empresas1` (`empresas_id`),
  KEY `fk_normas_empresas_normas1` (`normas_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

我需要创建一个触发器,在插入“empresas”后填充“articulos_empresas”,对于所有与新的“empresas”选择匹配的“aspectosambientals”中的“articulos”行。

使用此查询获取所有“articulos”。

SELECT articulos_id FROM aspectosambientales_articulos 
    WHERE  aspectosambientales_id = ID 
        -- ID is the aspectosambientales_id selected when the 'empresas' row is created
        --  maybe something like NEW.aspectosambientales_id

但我不知道如何在触发器中创建一个类似于“for循环”的循环,以处理查询结果中的每个值。

类似于以下内容:

CREATE TRIGGER 'filltableae' AFTER INSERT ON 'empresas' 
FOR EACH ROW
BEGIN 
DECLARE arrayresult = (SELECT articulos_id FROM aspectosambientales_articulos 
    WHERE  aspectosambientales_id = NEW.aspectosambientales_id)
--- here is when i have to do the loop for all the results
--- for ids in arrayresults
---  insert into articulos_empresas ('',NEW.id, ids, '', '' ,'','')
--- endfor
END

谢谢!!!

2个回答

46

根据@Razvan的答案,我在此留下触发器代码,希望能帮助到别人。

DROP TRIGGER IF EXISTS AEINST;
DELIMITER //
CREATE TRIGGER AEINST AFTER INSERT ON procesos_aspectos
FOR EACH ROW
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE ids INT;
    DECLARE cur CURSOR FOR SELECT articulos_id FROM aspectosambientales_articulos WHERE aspectosambientales_id = NEW.aspectosambientales_id;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN cur;
        ins_loop: LOOP
            FETCH cur INTO ids;
            IF done THEN
                LEAVE ins_loop;
            END IF;
            INSERT INTO articulos_empresas VALUES (null,ids, NEW.empresas_id,null,null,null,null);
        END LOOP;
    CLOSE cur;
END; //
DELIMITER ;

再次感谢!


很棒的答案,在生产和实践中非常有用。 - Mr. Crowley

10

它可以工作,如果我将其保存为一个过程,我可以将NEW.id作为过程的参数发送,如果需要在其他触发器中使用。 - elin3t
根据 @Razvan 的回答,我在这里留下了结果: - elin3t

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接