sql find_in_set在oracle下的解决方案


比如一张表: 
    artile (id,type,content); 
    type:1表示文艺类,2表示小说类,3表示传记,4表示传说,等等5,6,7,8 

    表数据: 
    id       type           content 
     1        3,1           dfasdfasdf 
     2        1,3,6,8       dfasdf 
     3        6,8,9         add 
     现在要找出3传记类的artile记录 

     mysql: select * from artile where find_in_set('3',type); 
     
     oralce 语句实现: 
     select * from artile da where instr(','||type||',',',3,')<>0; 
     (原理:将1,3,6,8转为 ,1,3,6,8,然后找出 ,3,的位置 
            将3,1转为 ,3,1,然后找出 ,3,的位置 
            则<>0的即为存在,返回记录) 

    用自定义一个find_in_set的oracle function 来解决 
    create or replace function find_in_set(arg1 in varchar2,arg2 in varchar) 
    return number is Result number; 
    begin 
    select instr(','||arg2||','  , ','||arg1||',') into Result from dual; 
    return(Result); 
    end find_in_set; 
    则:select * from artile where find_in_set('3',type)<>0; 

    mysql可接受0或其它number做为where 条件,oracle只接受表达式做为where 条件

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM