use test_db_foo_bar; create table filter (c1 varchar(255)); create table data   (c0 int, c1 varchar(255)); insert into filter (c1)    values ('a b c d'); insert into data   (c0,c1) values (1, 'x x x x'); insert into data   (c0,c1) values (2, 'a x x x'); insert into data   (c0,c1) values (3, 'a b x x'); insert into data   (c0,c1) values (4, 'a b c x'); insert into data   (c0,c1) values (5, 'a b c d'); delimiter // create procedure demo() begin   declare filter_c1,data_c1,str,tmp varchar(255);   declare len,pos,data_c0,cnt int default 0;   declare done BOOL default FALSE;   declare cur1 cursor for select c1 from filter;   declare cur2 cursor for select c0,c1 from data;   declare continue handler for not found set done := TRUE;   open cur1;   label1: loop      fetch cur1 into filter_c1;      if done then         leave label1;      end if;      open cur2;      label2: loop         fetch cur2 into data_c0, data_c1;         if done then            leave label2;         end if;         set tmp = filter_c1;         set cnt = 0;         label3: loop            set pos = instr(tmp, ' ');            set len = length(tmp);            if pos = 0 then               set str = concat('%',tmp,'%');            else               set str = concat('%',substr(tmp, 1, pos-1),'%');            end if;            set tmp = substr(tmp, pos+1, len);            if data_c1 like str then               set cnt = cnt+1;            end if;            if cnt = 2 then               select data_c0, data_c1;               leave label3;            end if;            if pos = 0 then               leave label3;            end if;         end loop label3;      end loop label2;      close cur2;   end loop label1;   close cur1; end // delimiter; call demo();