背景:
对电子商务网站中每个产品的url进行seo的优化,如果在seo表中商家自己设置了优化字段,则对该字段进行处理,如果没有设置,则对产品名字进行处理,然后把处理后的字符串对应到指定的产品。前提条件:
3张表:inv_product ,inv_product_seo,inv_category_product_url。
inv_product:表中有用字段有:id,name,companyid,status_id,product_type
inv_product_seo:表中有用字段:url,product_id
inv_category_product_url:表中有以下字段:id,category_product_id,url,company_id要求:
先查找inv_product_seo表中是否有url字段,如果url不为空并且部位空字符串,则对inv_product_seo表中的url字段进行处理,如果inv_product_seo表中url字段为空或者为空字符串,则处理inv_product表中的name字段。字符串处理要求:
把一切非字母数字的字符替换成“-”,例:“%$sfs #sf”替换成“sfs-sf”,“ ¥%yy-8il&%sf- ”替换成“yy-8il-sf”。注意:替换后的字符串前后不能有特殊字符,并且替换后的字符串只能包含数字,字母和“-”,并且只能以数字或字母开头和结束,相邻不能有多个“-”。处理完成后在inv_category_product_url中生成一条记录,处理后的字符串对应记录里的url。category_product_id对应inv_product表里的id,company_id对应inv_product表里的companyid, id在存储过程里生成。以下是我写的存储过程:
存储过程1: 主要是获取需要处理的字符串,然后调用存储过程2进行字符串的特殊处理,并生成记录。
DELIMITER $$DROP PROCEDURE IF EXISTS `asp`.`replacename`$$CREATE DEFINER=`root`@`localhost` PROCEDURE `replacename`()
begin
declare urlword varchar(255) default null;
declare id varchar(255);
declare b int default 0;
declare num int default 0;
declare productid char(32);
declare productid_cursor CURSOR for select id from inv_product where status_id='0000000000qqqqqq0000001' and product_type='ITEM';
declare continue handler for not found set b = 1;
open productid_cursor;
repeat
fetch productid_cursor into productid; select url into urlword from inv_product_seo where product_id=productid; if urlword != null then
set urlword = trim(urlword);
if urlword != '' then
call processurl(urlword,productid);
else
select name into urlword from inv_product where id=productid;
call processurl(urlword,productid);
end if;
else
select name into urlword from inv_product where id=productid;
call processurl(urlword,productid);
end if;
set b=1;
until b=1
end repeat;
close productid_cursor;
end$$DELIMITER ;存储过程1比较简单,我就不进行解释了:存储过程2:
DELIMITER $$DROP PROCEDURE IF EXISTS `asp`.`processurl`$$CREATE DEFINER=`root`@`localhost` PROCEDURE `processurl`(inout name char(255),inout productid char(32))
begin
declare companyid char(32);
declare urlword char(255);
declare alen int;
declare eurl char(255) default '';
declare estr char(32);
declare k int default 0;
declare t int default 1;
declare elen int;
declare sstr char;
declare cpid char(32) default null;
declare m int default 0;
declare endurl char(255); set urlword = name;
set alen = length(urlword); select company_id into companyid from inv_product where id=productid; repeat
set estr = substring(urlword,t,1);
select estr regexp '[a-zA-Z0-9]{1}' into k;
set t=t+1;
if k > 0 then
set eurl = concat(eurl,estr);
else
set elen = length(eurl);
if elen > 0 then
set estr = substring(eurl,elen,1);
if estr!='-' then
set eurl = concat(eurl,'-');
end if;
end if;
end if;
until t > alen
end repeat;
set elen = length(eurl);
set estr = substring(eurl,elen,1);
if estr = '-' then
set eurl = substring(eurl,1,elen-1);
end if; set endurl = eurl;
repeat
select id into cpid from inv_category_product_url where company_id=companyid and
url=endurl;
if cpid!=null then
set endurl = concat(eurl,m);
set m = m+1;
end if;
until cpid =null
end repeat; insert into inv_category_product_url value(replace(uuid(),"-",""),id,eurl,companyid);
end$$DELIMITER ;存储过程2:
我的设计思路是:对传过来的字符串进行处理,我是这样想的,单个截取字符串里的字符,进行正则匹配,然后对单个字符进行连接,最后组装成符合条件的字符串。 特别要注意的是处理后首尾不能是替换后的特殊字符(“-”)。
当在表 inv_category_product_url中创建新纪录是要须注意:可能处理后的字符串和 inv_category_product_url中的url字段重复,这时候必须对该字段进行处理,处理很简单就是最后加1 2 3...等等,反正就是每次加了数字需要再判断是否重复,如果重复再往后加更大的数字。做到这一步,执行该存储过程的时候好像有问题,貌似是死循环一样,表里也没新纪录。如果哪位同志能帮我看下哪里出里问题 感激不尽,谢谢!如果该存储过程能改进,请说明,谢谢!(我觉得对字符串的处理有点恶心)希望给的答案是亲自验证过的,再次谢谢!
对电子商务网站中每个产品的url进行seo的优化,如果在seo表中商家自己设置了优化字段,则对该字段进行处理,如果没有设置,则对产品名字进行处理,然后把处理后的字符串对应到指定的产品。前提条件:
3张表:inv_product ,inv_product_seo,inv_category_product_url。
inv_product:表中有用字段有:id,name,companyid,status_id,product_type
inv_product_seo:表中有用字段:url,product_id
inv_category_product_url:表中有以下字段:id,category_product_id,url,company_id要求:
先查找inv_product_seo表中是否有url字段,如果url不为空并且部位空字符串,则对inv_product_seo表中的url字段进行处理,如果inv_product_seo表中url字段为空或者为空字符串,则处理inv_product表中的name字段。字符串处理要求:
把一切非字母数字的字符替换成“-”,例:“%$sfs #sf”替换成“sfs-sf”,“ ¥%yy-8il&%sf- ”替换成“yy-8il-sf”。注意:替换后的字符串前后不能有特殊字符,并且替换后的字符串只能包含数字,字母和“-”,并且只能以数字或字母开头和结束,相邻不能有多个“-”。处理完成后在inv_category_product_url中生成一条记录,处理后的字符串对应记录里的url。category_product_id对应inv_product表里的id,company_id对应inv_product表里的companyid, id在存储过程里生成。以下是我写的存储过程:
存储过程1: 主要是获取需要处理的字符串,然后调用存储过程2进行字符串的特殊处理,并生成记录。
DELIMITER $$DROP PROCEDURE IF EXISTS `asp`.`replacename`$$CREATE DEFINER=`root`@`localhost` PROCEDURE `replacename`()
begin
declare urlword varchar(255) default null;
declare id varchar(255);
declare b int default 0;
declare num int default 0;
declare productid char(32);
declare productid_cursor CURSOR for select id from inv_product where status_id='0000000000qqqqqq0000001' and product_type='ITEM';
declare continue handler for not found set b = 1;
open productid_cursor;
repeat
fetch productid_cursor into productid; select url into urlword from inv_product_seo where product_id=productid; if urlword != null then
set urlword = trim(urlword);
if urlword != '' then
call processurl(urlword,productid);
else
select name into urlword from inv_product where id=productid;
call processurl(urlword,productid);
end if;
else
select name into urlword from inv_product where id=productid;
call processurl(urlword,productid);
end if;
set b=1;
until b=1
end repeat;
close productid_cursor;
end$$DELIMITER ;存储过程1比较简单,我就不进行解释了:存储过程2:
DELIMITER $$DROP PROCEDURE IF EXISTS `asp`.`processurl`$$CREATE DEFINER=`root`@`localhost` PROCEDURE `processurl`(inout name char(255),inout productid char(32))
begin
declare companyid char(32);
declare urlword char(255);
declare alen int;
declare eurl char(255) default '';
declare estr char(32);
declare k int default 0;
declare t int default 1;
declare elen int;
declare sstr char;
declare cpid char(32) default null;
declare m int default 0;
declare endurl char(255); set urlword = name;
set alen = length(urlword); select company_id into companyid from inv_product where id=productid; repeat
set estr = substring(urlword,t,1);
select estr regexp '[a-zA-Z0-9]{1}' into k;
set t=t+1;
if k > 0 then
set eurl = concat(eurl,estr);
else
set elen = length(eurl);
if elen > 0 then
set estr = substring(eurl,elen,1);
if estr!='-' then
set eurl = concat(eurl,'-');
end if;
end if;
end if;
until t > alen
end repeat;
set elen = length(eurl);
set estr = substring(eurl,elen,1);
if estr = '-' then
set eurl = substring(eurl,1,elen-1);
end if; set endurl = eurl;
repeat
select id into cpid from inv_category_product_url where company_id=companyid and
url=endurl;
if cpid!=null then
set endurl = concat(eurl,m);
set m = m+1;
end if;
until cpid =null
end repeat; insert into inv_category_product_url value(replace(uuid(),"-",""),id,eurl,companyid);
end$$DELIMITER ;存储过程2:
我的设计思路是:对传过来的字符串进行处理,我是这样想的,单个截取字符串里的字符,进行正则匹配,然后对单个字符进行连接,最后组装成符合条件的字符串。 特别要注意的是处理后首尾不能是替换后的特殊字符(“-”)。
当在表 inv_category_product_url中创建新纪录是要须注意:可能处理后的字符串和 inv_category_product_url中的url字段重复,这时候必须对该字段进行处理,处理很简单就是最后加1 2 3...等等,反正就是每次加了数字需要再判断是否重复,如果重复再往后加更大的数字。做到这一步,执行该存储过程的时候好像有问题,貌似是死循环一样,表里也没新纪录。如果哪位同志能帮我看下哪里出里问题 感激不尽,谢谢!如果该存储过程能改进,请说明,谢谢!(我觉得对字符串的处理有点恶心)希望给的答案是亲自验证过的,再次谢谢!
解决方案 »
- 由MYSQL SET IDENTITY_INSERT tablename ON;正确方法
- Transact-SQL 导出access
- 引用两个表的sql语句的问题
- 我用mysql不知道时间怎么设置的.
- 如何将txt文件导入mySql
- 经常在运行时出现window窗口,mysql异常中止
- mysql-front 能否向PHPmyadmin一样在网上在线管理数据库??
- (100分)在mysql中,select * from table1 LEFT JOIN table2 ON table1.id=table2.id,哪一个是左表,哪一个是右表,怎样使用 left joi
- MYSQL优化问题 难道这已经到极限了 还能怎么优化
- mfc用ado调用mysql存储过程插入中文数据时乱码
- 求助
- mysql 存储过程 不支持中文,有办法解决么?
cpid =null、cpid!=null
NULL<>NULL,用IS NULL OR COALESCE、NULLIF()等等函数判断