【问题标题】:Mysql Trigger Loop for query result with many rows多行查询结果的Mysql触发循环
【发布时间】:2012-08-10 04:55:39
【问题描述】:

您好,我有一个包含许多表和这样的外键的数据库

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”选择的“aspectosambientals”匹配的所有行的“articulos_empresas”。

我通过这个查询得到了所有的“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 loop”的循环

有些是这样的:

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

谢谢!!!

【问题讨论】:

    标签: mysql loops triggers multiple-results


    【解决方案1】:

    根据@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 ;
    

    再次感谢!

    【讨论】:

    • 很好的答案,在生产和实践中非常有用。
    【解决方案2】:

    据我所知,您可以使用游标遍历 SELECT 查询的结果。 见这里:http://dev.mysql.com/doc/refman/5.0/en/cursors.html

    【讨论】:

    • 它可以工作,如果我将 ir 保存为过程,如果需要在其他触发器中使用,我可以将 NEW.id 作为过程的参数发送
    • 基于@Razvan 的回答,我将结果留在这里:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2015-07-26
    • 2021-02-15
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多