Trigger does not recognize table (Trigger to break up content of NEW.values into multiple rows to insert into another table)

有些话、适合烂在心里 提交于 2019-12-08 10:13:47

问题


Error Code: 1109. Unknown table 'numbers' in field list

Why my code thinks that there is no table numbers and how to fix it?

And if possible answer question why use case with triggers?

P.S Numbers table I have been using combining with sunstring_index so from tables where in some column fields have two words i could split them into two rows

Maybe there is sufficient way?

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');

UPDATE: OP wants to create a trigger AFTER INSERT to break up content of NEW.values inserted into table1 into different rows and insert them into 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

break it up into rows and insert into table2 like below

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:


if you're just trying to break out the strings, you can just hardcode the 1 and 2 in there like this and there's no need to grab 1 and 2 in numbers table since that table is currently hardcoded to contain 1 and 2 anyways.

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

sqlfiddle

but then i noticed you don't even need to call SUBSTRING_INDEX() twice..this works too

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

sqlfiddle

UPDATE after seeing your comment, I see why you wanted to create table numbers so your trigger would be something like this. First you create table numbers that contains rows that has n values from 1 to 10 (possible maximum number of fields to break up into rows). Then you select from numbers where n values are <= number of fields in your number. Then apply SUBSTRING_INDEX() functions to get the field at n position.

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



来源:https://stackoverflow.com/questions/36805897/trigger-does-not-recognize-table-trigger-to-break-up-content-of-new-values-into

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!