求助sql的搜索语句 要求是按输入每个英文单词的首字母搜索,例如输入:hwg 搜索到 here we go,但是不能搜索到here we not go,求大神解答~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 本人也是小菜,不过有一点思路,供你参考一下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、拼接获取到的字母跟传入的搜索参数进行比较 可以使用 regexp 来实现。 没时间写注释,你看看行不行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$$ mysql安全问题(熟悉这方面的应该很简单) MySQL的正则中,“\”转义的问题 mysql有没有什么设置能 登记 错误的sql? 写了一个MySQL连接测试,不行,帮看看 请教各位,有一数据表中的字段有部分为中文,偶现在要把所有中文的替换掉怎么办? MYSQL 读记录,只能读前7个字段,后面的字段内容为空? 差异!! 多表查询时 FOR UPDATE的作用范围 数据表关联问题 >>我在万网的虚拟主机没有MySQL数据库支持,我想自己开启一个SQL数据库支持,谁能提供有用的信息,进来详细说!! mysql数据表达到千万级别的查询 求助 sql求和,重复
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、拼接获取到的字母跟传入的搜索参数进行比较
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
$$