我有个这样的数据表, 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

解决方案 »

  1.   

    感觉表设计有点问题,比如
    1    aaa      1-2,4-9,371    aaa      1
    1    aaa      2
    1    aaa      4
    ...............
    1    aaa      9这样查询很简单,否则同样要拆分字符串,以,为分隔,取-前后字段内容,再做比较
      

  2.   

    如果是要算法应该可以找到,
    如果仅限于用SQL语句则很难实现。毕竟这个atricles 字段并不属数据库的范式要求(连1NF都无法满足。)基于现在这个设计,大家一起看看程序中有什么好的算法。
      

  3.   

    mysql> select * from text;
    +----+-------+------------+
    | id | value | articles   |
    +----+-------+------------+
    |  1 | aaa   | 1-2,4-9,37 |
    |  2 | bbb   | 23,80      |
    |  3 | ccc   | 1-24       |
    +----+-------+------------+
    3 rows in set (0.00 sec)
    mysql>  select * from text t where id in (select substring(articles,1,instr(arti
    cles,",")-1) from t);
    +----+-------+------------+
    | id | value | articles   |
    +----+-------+------------+
    |  1 | aaa   | 1-2,4-9,37 |
    +----+-------+------------+
    1 row in set (0.00 sec)
    这个查询之简单匹配 通过拆分字符串 第一个“,”前的字符 ,即id=1 1在“ 1-2,4-9,37 ”存在但是要满足你想要的需求要做大量的sql语句 
    需要存储过程实现 但是还是建议2楼说的 你表设计有问题
      

  4.   

    写个函数,把 articles 字段值作为参数传进去处理进可以了
    函数内部根据“,”和“-”来判断处理就可以了,若存在的,函数返回1,否则返回0 
      

  5.   

    http://topic.csdn.net/u/20090727/09/fa250237-de1c-4266-be9a-99e9ac454d44.html看看这个帖子#24的。我觉得你这个问题貌似跟哪个问题有个类似!
      

  6.   

    比较难,不会是把articles 字段值作为参数传到数组,再用split()函数,感觉好麻烦
      

  7.   

    SELECT `values` FROM (
    SELECT newb,a1.values,
    IF(INSTR(newb,'-')>0,LEFT(newb,INSTR(newb,'-')-1),newb) AS mi,
    IF(INSTR(newb,'-')>0,MID(newb,INSTR(newb,'-')+1,LENGTH(newb)),newb) AS ma
    FROM (
    SELECT a.*,
    MID(MID(newa,b.id,LENGTH(newa)),2,LOCATE(',',MID(newa,b.id,LENGTH(newa)),2)-2) AS newb
     FROM (
    SELECT *,CONCAT(',',articles,',') AS newa FROM `test`.tn) a,lsb2 b
    WHERE LENGTH(newa)>=b.id
    AND MID(newa,b.id,1)=',' 
    AND LENGTH(MID(newa,b.id,LENGTH(newa)))>=2
    ORDER BY a.values,b.id) a1  ) a2
    WHERE 5 BETWEEN mi AND ma
    其中LSB2只有ID字段,内容1-1000数字,`test`.tn为工作表
      

  8.   

    哈,楼上又来这些“奇强”语句了个人觉得这些还是写个函数处理好,都写在一个SQL语句里面看得有点痛苦
      

  9.   

    假设格式固定,且articles只有一个“-”
      

  10.   

    反正是个全表扫描。把查询出来的结果放到datetable中。。逐行验证。。验证不通过的从datetable中删除。
      

  11.   

    这个很easy呀。下面是最直接最傻瓜的代码。
    不知道你的操作语言是什么,来个类似于C的伪代码好了:
    for(id=第一个,id<=最后一个,id++)
    {//不遍历是不行的字符串 strArt=该行articles 。
    //when i=1,strArt=1-2,4-9,37 //第1种可能:目标编号==37
    将目标编号转换为字符串,在strArt中查找
    ,如果有就是找到了,将该id加入结果。//第2种可能:目标编号==7
    for(从strArt第一个字符到最后一个循环,设当前的字符为c)
    {
    if(c==‘-’)
    {
    以(字符串前边界,字符串后边界,逗号)为边界,
    取c前后的字符,将这个字符串转换为int型得到iBef,iAft。
    if(目标编号>iBef and 目标编号<iAft)
    {
    找到了!将该id加入结果
    }
    }}
      

  12.   

    怎么不能缩进?,再来一遍
    for(i=id第一个,i<=id最后一个,id++)
    {//不遍历是不行的 字符串 strArt=该行articles 。
    //when i=1,strArt=1-2,4-9,37  //第1种可能:目标编号==37
    将目标编号转换为字符串,在strArt中查找
    ,如果有就是找到了,将该id加入结果。 //第2种可能:目标编号==7
    for(从strArt第一个字符到最后一个循环,设当前的字符为c)
    {
    if(c==‘-’)
    {
    以(字符串前边界,字符串后边界,逗号)为边界,
    取c前后的字符,将这个字符串转换为int型得到iBef,iAft。
    if(目标编号>iBef and 目标编号<iAft)
    {
    找到了!将该id加入结果
       }
    } }
    }
      

  13.   

    articles 
      1    aaa      1-2,4-9,37 
    这个设计数据库的该打PP连第一泛式,1NF,原子不可分性都不遵守!~~
      

  14.   

    数据库表名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
      

  15.   

    很强悍的SQL语句,如果懂得数据库设计范式,也就没有以上问题了
      

  16.   

              不会ing