触发器无法识别表(触发器将NEW.values的内容分解为多行以插入另一个表中)

时间:2016-04-23 02:15:38

标签: php mysql triggers mysql-workbench

  

错误代码:1109。未知的表格'数字'在字段列表中

为什么我的代码认为没有表号,如何解决?

如果可能的话,回答问题为什么用触发器使用案例?

P.S Numbers表我一直在使用与sunstring_index结合,所以从表格中,在某些列字段中有两个单词,我可以将它们分成两个

也许有足够的方法?

drop schema exp;
create database exp;
use exp;
create table IDVU (
`ID` int(8)  unsigned not null auto_increment ,
`VU` varchar(45) not null,
PRIMARY KEY (`id`),
KEY `ix_VU` (`VU`)
)ENGINE = InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

create table sep (
ID1 int(8) unsigned NOT NULL primary key auto_increment,
ID2 int(8) unsigned not null,
V varchar(45) not null,
U varchar(45) not null,
KEY `ix_ID2` (`ID2`),
CONSTRAINT `ID_IDVU_SEP` FOREIGN KEY (`ID2`) REFERENCES `IDVU` (`ID`)
ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE = InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

create table numbers select 1 n union all select 2;
delimiter $$
CREATE TRIGGER `edit` AFTER INSERT 
ON `idvu` 
FOR EACH ROW BEGIN 

    IF new.VU like '% %' THEN 
        SET @V = SUBSTRING_INDEX(SUBSTRING_INDEX(new.Vu, ' ', numbers.n), ' ', -1),
            @U = SUBSTRING_INDEX(SUBSTRING_INDEX(new.Vu, ' ', numbers.n), ' ', -1);
    else
        SET @V = 'NEW',@U = 'NEW';
    END IF;

    INSERT INTO sep (ID2,V, U) VALUES (new.ID,@V, @U);

END$$

delimiter ;

select * from idvu order by ID;
select * from sep order by ID1;
insert into iDVU value (2,'Dd Rr');

更新 OP希望创建一个触发器AFTER INSERT,将插入table1的NEW.values内容分解为不同的行,并将其插入table2

Table1
Number  Player    Team              Position
  1     Jan Ho    Team 1            C
  2     Mike Dog  Team 3            LW
4 8     Slim Dre  Team 4, Team 1    G D
  6     Mad Dog   Team 2            D

将其分成行并插入table2,如下所示

Table2
Number  Player    Team              Position
  1     Jan Ho    Team 1            C
  2     Mike Dog  Team 3            LW
  4     Slim Dre  Team 4            G
  8     Slim Dre  Team 1            D
  6     Mad Dog   Team 2            D

1 个答案:

答案 0 :(得分:0)

如果你只是试图打破字符串,你可以像这样硬编码那里的1和2,并且不需要在数字表中抓取1和2,因为该表当前是硬编码的,包含1和2反正。

SET @V = SUBSTRING_INDEX(SUBSTRING_INDEX(new.Vu, ' ', 1), ' ', -1),
    @U = SUBSTRING_INDEX(SUBSTRING_INDEX(new.Vu, ' ', 2), ' ', -1);

sqlfiddle

然后我注意到你甚至不需要两次调用SUBSTRING_INDEX()..这也有效

    SET @V = SUBSTRING_INDEX(new.Vu, ' ', 1),
        @U = SUBSTRING_INDEX(new.Vu,' ', -1);

sqlfiddle

看到你的评论后,

更新,我知道为什么你要创建表numbers,所以你的触发器会是这样的。 首先创建表numbers,其中包含n值为1到10的行(可能的最大字段数分成行)。 然后从numbers中选择n值为< =数字中的字段数。然后应用SUBSTRING_INDEX()函数将字段置于n位置。

create table numbers 
select 1 as n 
union select 2 
union select 3
union select 4
union select 5
union select 6
union select 7
union select 8
union select 9
union select 10;

CREATE TRIGGER `edit2` AFTER INSERT 
ON `table1` 
FOR EACH ROW BEGIN 

    INSERT INTO table2 (number,player,team,position)
    SELECT 
    SUBSTRING_INDEX(SUBSTRING_INDEX(NEW.number,' ',n),' ',-1) as number,
    NEW.player as player,
    SUBSTRING_INDEX(SUBSTRING_INDEX(NEW.team,', ',n),', ',-1) as team,
    SUBSTRING_INDEX(SUBSTRING_INDEX(NEW.position,' ',n),' ',-1) as position
    FROM
    numbers n
    WHERE LENGTH(NEW.number)
             - LENGTH(REPLACE(NEW.number,' ',''))
             + 1 >= n.n;

END

sqlfiddle to see trigger in action