要求是按输入每个英文单词的首字母搜索,例如输入:hwg 搜索到 here we go,但是不能搜索到here we not go,求大神解答~

解决方案 »

  1.   

    本人也是小菜,不过有一点思路,供你参考一下1、定义游标获取数据,并循环输出
    2、循环内获取数据包含空格数:select length(replace('here we go',' ','  '))-length('here we go');
    3、根据包含的空格数循环获取参数的每段英文
          select substring_index(substring_index('here we go',' ',i),' ',-1);
    4、获取每段英文的第一个字母:select substring('here we go',1,1);
    5、拼接获取到的字母跟传入的搜索参数进行比较
      

  2.   

    可以使用 regexp 来实现。
      

  3.   

    没时间写注释,你看看行不行
    CREATE TABLE `test3` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8_bin',
    PRIMARY KEY (`id`)
    );insert into test3(name)values
    ('chen zhen'),
    ('li xiao long'),
    ('ha ha'),
    ('xi xi');delimiter $$
    CREATE DEFINER=`root`@`localhost` PROCEDURE `pro_test`(IN `ttt` varchar(50))
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
    BEGIN declare not_exists int default 0;

    declare $_names varchar(100); #姓名集合

    declare $_name  varchar(50); #姓名拆分

    declare $_first varchar(10); #首字母合集

    declare $_error int default 0; declare $_num int default 0; #需要循环的次数,也就是字符串包含‘,’号的数量

    declare i  int default 0; #循环变量 declare test_cur cursor for select name from test3;

    declare continue handler for sqlexception set $_error = 1;

    declare continue handler for not found set not_exists = 1;

    open test_cur;

    while(not_exists!=1)do

    fetch test_cur into $_names;

    if(not_exists!=1)then if(LOCATE(' ',$_names)<>0) then

    /*********获取参数中包含多少个‘,’号**********/
    SET $_num=LENGTH(REPLACE($_names,' ','  '))-LENGTH($_names);

    set i =0;

    set $_first = '';
    while i<=$_num do

    set i=i+1;

    set $_name=SUBSTRING_INDEX(SUBSTRING_INDEX($_names,' ',i),' ',-1);

    set $_first = concat($_first,substring($_name,1,1));

    end while;

    if($_first=ttt)then

    select $_names;

    end if;

    end if;

    end if; end while;


    close test_cur;


    if($_error=1)then
    rollback;
    else
    commit;
    end if;

    /********返回执行结果 0成功,1失败*******/
    SELECT $_error AS errorCode;

    END
    $$