解决方案 »

  1.   

    这些数据啥含义
    那么字段下abc是字段名还是b表中 name的值
    code字段是干啥用的,排序用吗?
      

  2.   

    拼出来的语句是 select * from b where b.code = 331 or b.code =449 这样后面的where条件拼的是一个name下所以语句
      

  3.   

    同一个name下operator、logic是一样的?看你的语句拼接成in 或not in效率更好一点
    select 'select * from b where code '||decode(max(operator),'=','in','!=','not in')||' ('||wmsys.wm_concat(code)||')' 
    from a where name='abc'
      

  4.   

    那a表应该还得有个排序的字段才行,否则and or顺序随便,查询结果可是多种多样的
    a and b or c和a and c or b这结果能一样吗?
    另外比如有3个条件,只需要有两个logic用于关联,多出来的是哪个,怎么判断?
      

  5.   

    有时候 拼出来 还会是select * from b where b.code = 331 or b.code !=449 or b.code like '%code%'这样, 
      

  6.   

    对,如果or and 乱序就会出问题了,那就只管or的,不管and的,出现and的单独判断处理好了
      

  7.   


    -- 11.2 下,你可以试试 listagg 函数。
    create table m as 
    select 'cc'  name , 'and' logic , '=' op ,'555' as code from dual union all
    select 'cc'  name , 'and' logic , '=' op ,'444' as code from dual union all
    select 'name'  name , 'and' logic , '=' op ,'100' as code from dual union all
    select 'name'  name , 'and' logic , '=' op ,'101' as code from dual union all
    select 'name'  name , 'and' logic , '=' op ,'111' as code from dualwith mt as (
    select  listagg(' ' || name || op || code || ' ' || logic  ,'')   within group (order by name) c
    from m where name = 'name'
    )
    select substr(c,1,length(c) -4 )   from mt 
      

  8.   

    先把or的拼接起来,然后再与and进行拼接select listagg(str,' and ') within group(order by rownum) from(
        select '('||listagg('code'||operator||code,' or ') within group(order by rownum)||')'  str
        from a where name='abc' and logic='or'
        union all
        select 'code'||operator||code str
        from a where name='abc' and logic='and'
    )