循环逗号分隔值并在mySQL中使用触发器后插入

时间:2016-09-19 01:49:39

标签: mysql triggers separator

请帮助,我需要在mysql中使用逗号分隔符值创建循环触发器。

示例表tbl_migrasi

id_tps |   suara   |
------ | ----------|
   1   | 2,15.3,35 |
   2   | 2,14.3,34 |

尝试我的触发器

DROP TRIGGER IF EXISTS `trigger_migrasi`;
    DELIMITER //
    CREATE TRIGGER `trigger_migrasi` AFTER INSERT ON `tbl_migrasi`
     FOR EACH ROW BEGIN
     BEGIN
     SET @mydata= (select suara from tbl_migrasi WHERE suara=NEW.suara order by migrasi_id desc limit 1);
     IF @dataku IS NOT NULL THEN
       SET @total= (select LENGTH(suara) - LENGTH(REPLACE(suara, ',', '')) AS total from tbl_migrasi WHERE suara=NEW.suara ORDER BY migrasi_id DESC LIMIT 1);
       SET @x = 1;
       WHILE @x < @total DO
       INSERT INTO tbl_rekapitulasi
       SET @x = @x + 1;
       SET @y = @x + 1;
         (tps_kode, kandidat_id,rekapitulasi_suara)
         VALUES
         (NEW.migrasi_tps,SPLIT_STR(SPLIT_STR(@mydata, ',', @x), '.', @x),SPLIT_STR(SPLIT_STR(@mydata, ',', @y), '.', @x));
       END WHILE;
     END IF;
     END
    END

我需要输出表tbl_kandidat

id_tps | kandidat_id | suara |
------ | ----------- | ----- |
   1   |      2      |  15   |
   1   |      2      |  14   |
   2   |      3      |  35   |
   2   |      3      |  34   |

0 个答案:

没有答案