Mysql,全文搜索的存储过程产生部分正确的结果

时间:2016-10-13 21:33:55

标签: mysql loops stored-procedures full-text-search

我使用循环在mysql中创建了以下表和存储过程。它似乎部分有效。

DROP table if EXISTS x; 
DROP table if exists y; 
DROP TABLE if EXISTS z; 

CREATE TABLE x (`code` VARCHAR(10) not null primary key, 
description text, fulltext key(description));

INSERT INTO x
VALUES(2111,'rice, husked'),
('0113','rice, paddy'),
('0124','fish, fresh'),
(2225,'beef meat'),
('0114','bananas'),
('0115','mango');

CREATE TABLE y (section text not null, `code` text);

INSERT INTO y
values('food', 'rice local'),
('food', 'rice imported'),
('food', 'beer'),
('food', 'banana');

create table z (section text not null, `code` text, cpc VARCHAR(10)   
NULL);

drop procedure if exists fmatch;
delimiter //
create procedure fmatch()
language sql
deterministic
sql security definer
begin

declare v_code VARCHAR(10);
declare v_description text;
declare v_finished int;
    declare c cursor for select * from x ;
declare continue handler for not found set v_finished=1;

delete from z;

open c;
 c_loop: loop
   fetch c into v_code,v_description;
  if v_finished then
    leave c_loop;
  end if;

insert into z 
        select y.section, y.`code`, v_code
 from y where match (y.`code`) against (v_description in boolean mode);
end loop c_loop;
close c;

select * from z;
end//
delimiter ;

call fmatch();

此处产生的结果为:

section   code             cpc

food      rice local       2111
food      rice imported    2111
food      rice local       0113
food      rice imported    0113

相反,我希望结果表为:

section   code             cpc

food      banana           0114
food      beer             null
food      rice local       0113
food      rice imported    0113

我在错误的地方寻求你的建议。

1 个答案:

答案 0 :(得分:0)

我使用不同的数据库进行了上述映射练习。以下是两个数据库 - 一个来自调查数据,另一个来自联合国分类系统。我试图将调查数据(关于各种支出项目)与联合国分类系统中的数据进行匹配。

然而,由于原始数据是具有不同命名的原始意义,因此结果(匹配)并不那么好。即使是下面的过程也不会产生部分单词匹配,例如芒果(来自原始数据)未映射到芒果。

在布尔模式中,您可以包含match(column1,column2 ..)(布尔模式下的'mango *'),它将产生部分字匹配。以类似的方式,我想在下面的代码中的列v_code上加上星号,但这不起作用。如果有人有其他想法,最受欢迎。

 ALTER TABLE dbo_CPC
 ADD FULLTEXT(`CODE`, description); 

 ALTER TABLE SLE11_MCPC
 ADD FULLTEXT(`CODE`, Section, MCPC); 

 drop procedure if exists SLE11_CPC_Mapping;
 delimiter //
 create procedure SLE11_CPC_Mapping()
 language sql
 DETERMINISTIC
 sql security definer
  begin
  declare v_section VARCHAR(10);
  declare v_code text;
  declare v_mcpc VARCHAR(10); 
  declare v_finished int;
   declare c cursor for select * from SLE11_MCPC;
   declare continue handler for not found set v_finished=1;

  DELETE from SLE11_MCPC; 

    open c;
  c_loop: loop
   fetch c into v_section, v_code, v_mcpc; 
   if v_finished then
    leave c_loop;
end if;

  insert into SLE11_MCPC 
        select v_section, v_code, left(dbo_CPC.`code`,4)
 from dbo_CPC where match (dbo_CPC.Description) against (v_code in 
    boolean MODE)
     LIMIT 1;
end loop c_loop;
close c;

 end//
delimiter ;