前几天发了一个帖子询问:SQL如何字段分割后经行比较并且取出相应的记录===========
假设有表A,A的结构如下:表Aid str1 str2
1 1,3,5 4,8
2 1,4 4,7,10我要的结果是:字段str1分割后的数字没有一个存在于str2中,那么就把这条记录取出来。
比如id为1的记录,str1分割后是1和3和5,这三个数字分别不属于str2中,所以id为1的记录取出来。
而id为2的记录,str1分割后是1和4,而4属于str2,那么id为2的记录不取出来。希望高手们给小弟一个正确的sql语句,感激万分,万分感激。
============很多高手给了sqlserver的SQL语句。
http://topic.csdn.net/u/20100621/20/aa502169-70be-4b90-a6b9-02dde5370449.html但是客户突然改变要求,要用Oracle当数据库,所以提供的SQL语句不管用了,
希望高手们再帮小弟一把。再次感激不尽。

解决方案 »

  1.   

    麻烦能不能发个能执行的语句参考一下,好像instr()无法实现我的需求。
      

  2.   


    drop table test;
    create table test (id number, str1 varchar2(200), str2 varchar2(200));insert into test values(1, '1,3,5', '4,8');
    insert into test values(2, '1,4', '4,7,10');
    commit;select * from test
    where not regexp_like(',' || str2 || ',', ',(' || replace(str1, ',', '|') || '),');        ID STR1                 STR2                
    ---------- -------------------- --------------------
             1 1,3,5                4,8          
      

  3.   

    select id, str1, str2, 
           translate(replace(str1,',',''),replace(str2,',',''),'$') as str3
    from a
    where translate(replace(str1,',',''),replace(str2,',',''),'$') like '$%';
      

  4.   

    select id, str1, str2, 
           translate(replace(str1,',',''),replace(str2,',',''),'$') as str3
    from a
    where translate(replace(str1,',',''),replace(str2,',',''),'$') not like '$%';
      

  5.   

    这种方法不太准确吧
    看下面的例子drop table test;
    create table test (id number, str1 varchar2(200), str2 varchar2(200));
    insert into test values(1, '1,3,5', '4,8');
    insert into test values(2, '1,4', '4,7,10');
    insert into test values(3, '1,4', '4,7,9');
    insert into test values(4, '2,8', '4,78,9');
    commit;select id, str1, str2, 
           translate(replace(str1,',',''),replace(str2,',',''),'$') as str3
    from test
    where translate(replace(str1,',',''),replace(str2,',',''),'$') not like '$%';        ID STR1                 STR2                 STR3                
    ---------- -------------------- -------------------- --------------------
             1 1,3,5                4,8                  135                 
             3 1,4                  4,7,9                1$                  
             4 2,8                  4,78,9               2     
    -- 按楼主的要求,ID为3的那条记录不应该查询出来的-- 10g和11g用正则会准确一点:
    select id,str1,str2
    from test
    where not regexp_like(',' || str2 || ',', ',(' || replace(str1, ',', '|') || '),');        ID STR1                 STR2                
    ---------- -------------------- --------------------
             1 1,3,5                4,8                 
             4 2,8                  4,78,9  
      

  6.   


    -- 前、后都加个百分号模糊查询,不就得了?select id, str1, str2, 
           translate(replace(str1,',',''),replace(str2,',',''),'$') as str3
    from test
    where translate(replace(str1,',',''),replace(str2,',',''),'$') not like '%$%';
      

  7.   

    -- 这样更好(当然,我只是说在我的方法中):select id, str1, str2, 
           translate(replace(str1,',',''),replace(str2,',',''),',') as str3
    from test
    where translate(replace(str1,',',''),replace(str2,',',''),',') not like '%,%';