求一个算法~~~~

changliushun 2009-09-08 04:24:03
加精
我有个这样的数据表, articles下面是文章id号码 (mysql数据库)
要求结果:根据给定的文章id号 查出 id values

比如 articles给出的是 5 那么结果应该是 第一行and第三行

id values articles
1 aaa 1-2,4-9,37
2 bbb 23,80
3 ccc 1-24
...全文
1368 81 打赏 收藏 转发到动态 举报
写回复
用AI写文章
81 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaolzz 2010-03-28
  • 打赏
  • 举报
回复
真不知道你怎么想的,居然会有这样的奇思妙想。不过如果真的要写的话,拆分时除了以“-”为分隔符的同时,是不是还得以数字作为分割符。因为你说id=5,23也满足。如果是这样的话,那就晕了 。
jacky1977 2009-09-13
  • 打赏
  • 举报
回复
不错。。。。。。。。
etongchina 2009-09-13
  • 打赏
  • 举报
回复
很强悍的SQL语句,如果懂得数据库设计范式,也就没有以上问题了
sanhuang 2009-09-13
  • 打赏
  • 举报
回复
帮不了忙~
jinpingbai 2009-09-13
  • 打赏
  • 举报
回复
好像有点不清楚
qwer11123 2009-09-13
  • 打赏
  • 举报
回复
不会ing
小童012 2009-09-12
  • 打赏
  • 举报
回复
学习数据库知识中
c184977277 2009-09-12
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分!能不能多加几分啊
zhuzhangliang 2009-09-12
  • 打赏
  • 举报
回复
不懂啊
ellenluo 2009-09-12
  • 打赏
  • 举报
回复
又有收获了
xue1225go 2009-09-11
  • 打赏
  • 举报
回复
数据库表名a1,字段名为id,values,articles.


用两个函数,一个存储过程可以做出来!

函数一:func_get_split_string
CREATE FUNCTION `func_get_split_string`(
f_string varchar(1000),f_delimiter varchar(5),f_order int) RETURNS varchar(255) CHARSET utf8
BEGIN
declare result varchar(255) default '';
set result = reverse(substring_index(reverse(substring_index(f_string,f_delimiter,f_order)),f_delimiter,1));
return result;
END

函数二:func_get_split_string_total
CREATE FUNCTION `func_get_split_string_total`(
f_string varchar(1000),f_delimiter varchar(5)
) RETURNS int(11)
BEGIN
return 1+(length(f_string) - length(replace(f_string,f_delimiter,'')));
END

存储过程一:
CREATE PROCEDURE `useCursor`(in articleId int)
BEGIN
declare tmpId int;
declare tmpArticles varchar(255) default '' ;
declare allId varchar(255) default '' ;
declare allArticles varchar(255) default '' ;
declare i INT DEFAULT 0;
declare j INT DEFAULT 0;
declare i_count int;
declare j_count int;
declare beginNum int;
declare endNum int;
declare sSql varchar(4000);
declare cur1 CURSOR FOR SELECT id,articles FROM a1;
declare CONTINUE HANDLER FOR SQLSTATE '02000' SET tmpId = null;

set sSql = 'select * from a1 where';
OPEN cur1;
FETCH cur1 INTO tmpId,allArticles;
WHILE (tmpId is not null) DO
SELECT func_get_split_string_total(allArticles,',') into i_count;
set i = 0;
while i < i_count do
set i = i + 1;
select func_get_split_string(allArticles,',',i) into tmpArticles;
SELECT func_get_split_string_total(tmpArticles,'-') into j_count;
if (j_count =1) then
select func_get_split_string(tmpArticles,'-',1) into beginNum;
if (beginNum = articleId) then
set allId = CONCAT(allId ,tmpId,',') ;
set sSql = CONCAT(sSql,' id = ',tmpId,' or');
end if;
end if;
if (j_count = 2) then
select func_get_split_string(tmpArticles,'-',1) into beginNum;
select func_get_split_string(tmpArticles,'-',2) into endNum;
if (articleId >= beginNum and articleId <= endNum) then
set allId = CONCAT(allId ,tmpId,',') ;
set sSql = CONCAT(sSql,' id = ',tmpId,' or');
end if;
end if;
end while;
FETCH cur1 INTO tmpId,allArticles;
END WHILE;
CLOSE cur1;
if (length(allId) > 0) then
set sSql = substring(sSql,1,length(sSql)-2);
end if;
set @sQuery = sSql;
prepare stmt from @sQuery;
execute stmt;
END
lichun510 2009-09-10
  • 打赏
  • 举报
回复
值得借鉴。
yanguobin2004 2009-09-10
  • 打赏
  • 举报
回复
把表设计成傻比样,还要别人解决!这种思路最害人!
xiaojiang222 2009-09-10
  • 打赏
  • 举报
回复
学习下,
blackman6688 2009-09-10
  • 打赏
  • 举报
回复
好像挺难的,不过看看有没有高手啊。
liusong721 2009-09-10
  • 打赏
  • 举报
回复
我也来学习下!
color_zy 2009-09-10
  • 打赏
  • 举报
回复
学习下
fxltsblsds 2009-09-10
  • 打赏
  • 举报
回复
顶顶顶!结束了。
超级大笨狼 2009-09-10
  • 打赏
  • 举报
回复
articles
1 aaa 1-2,4-9,37
这个设计数据库的该打PP

连第一泛式,1NF,原子不可分性都不遵守!~~
livyhoney 2009-09-09
  • 打赏
  • 举报
回复
呵呵,相当不错,高手如云呐
加载更多回复(59)

56,675

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧