2012-08-13 70 views
7
查詢結果

您好,我有很多的表像這樣mysql的觸發迴路與許多行

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」與此查詢

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 

謝謝!!!

回答

8

據我所知,你可以迭代通過使用光標的SELECT查詢的結果。 在這裏看到:http://dev.mysql.com/doc/refman/5.0/en/cursors.html

+0

它的工作原理,如果我保存ir作爲一個過程,我可以發送NEW.id作爲過程的參數,如果需要在其他觸發器中使用 – elin3t 2012-08-13 23:53:26

+0

基於@Razvan答案我在這裏留下了結果: – elin3t 2012-08-14 00:59:39

28

基於@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 ; 

再次感謝!

+0

很好的回答,在生產和實踐中非常有用。 – 2016-09-27 14:12:39