有三个表:users,ap_name_bank_m和ap_name_bank_h。用户表有三列' name_eng' (英文用户名),' name_gdn_eng'(用户英文监护人姓名)和'类别'。
ap_name_bank_m和ap_name_bank_h是每个名称类别的名称语料库:' m'或者' h'。
当一行更新时,我想检查两个表中的每个单词是否存在于两个表ap_name_bank_m和ap_name_bank_h中的任何一个。
分配的类别越高。我写的下面的代码进入了无限循环,我得到了#34; MySQL服务器已经消失了错误"。有人能告诉我哪里错了吗?
假设name_eng和name_gdn_eng只包含带空格的单词,而不包含任何其他单词。
DELIMITER $$
create trigger set_cat before update on users_table for each row
BEGIN
declare words text;
declare word varchar(50);
declare num_m int default 0;
declare num_h int default 0;
declare len int default 0;
set words = concat(new.name_eng,' ',new.name_gdn_eng);
iterator:
LOOP
set word = substring_index(words,' ',1);
set num_m = EXISTS(select 1 from ap_name_bank_m where name=word) + num_m;
set num_h = EXISTS(select 1 from ap_name_bank_h where name=word) + num_h;
set words = trim(replace(words,word,''));
END LOOP iterator;
if (num_m > num_h) then set new.category='M'; end if;
if (num_h > num_m) then set new.category='H'; end if;
END $$
DELIMITER ;
答案 0 :(得分:0)
我找到了一个更好的方法来做到这一点。不需要循环。
BEGIN
declare words text;
declare word varchar(50);
declare num_m int default 0;
declare num_h int default 0;
set words = concat(new.name_eng,' ',new.name_gdn_eng);
set num_h = (select count(*) from ap_name_bank_h where match(name) against (words in natural language mode));
set num_m = (select count(*) from ap_name_bank_m where match(name) against(words in natural language mode));
/*
iterator:
LOOP
set word = substring_index(words,' ',1);
set num_m = EXISTS(select 1 from ap_name_bank_m where name=word) + num_m;
set num_h = EXISTS(select 1 from ap_name_bank_h where name=word) + num_h;
set words = trim(replace(words,word,''));
END LOOP iterator;*/
if (num_m > num_h) then set new.category='M'; end if;
if (num_h > num_m) then set new.category='H'; end if;
END