我现在有一张基表暂时叫做表A吧(表字段大概有 area_no,is_pps,is_iess,is_employ,pay_fee等等)的数据需要一些条件进行过滤。
过滤的条件是通过在网页上的条件配置生成的一张条件表(表B)从而对表A的数据进行过滤。表A 字段如下:customer_id area_no is_pps is_iess is_employ  pay_fee123456       810       1       1        1        20
123447       810       0       1        0        0
123457       810       0       1        0        50
123458       811       0       1        0        100
123459       811       0       0        0        50
123467       812       0       1        0        30
123468       812       0       1        1        100
123469       812       0       0        0        100表B为:map_filed_name 意思为对应A表的字段名
map_filed_value意思为对应A表中相应字段的值
map_filed_type决定我们查询语句中使用何种查询方式  比如:01代表使用这个条件的时候要使用‘=’ ,02 代表使用 ‘in’,05代表写查询语句是我们需要使用‘>=’和‘<=’
map_filed_name    map_filed_value   map_filed_type
area_no               810                02
area_no               811                02
is_iess                1                 01
is_pps                 0                 01
is_employ              0                 01  
pay_fee               10                 05     
pay_fee               200                05  
最后的结果应该为
123457       810       0       1        0        50
123458       811       0       1        0        100
不知道高手们清楚我的问题没~~~~谢谢!!我写了点  但是有很多地方还没想通的~~~应该是用动态SQL吧    请高手们多多指教!!!谢谢

解决方案 »

  1.   

    select ' and '||map_filed_name||' = ' ||map_filed_value 
    from b where map_filed_type = '01'
    union 
    select ' and '||map_filed_name||' in ('||wm_sys.wm_concat(map_filed_value)||' )' 
    from b where map_filed_type = '02' group by map_filed_name
    union
    select ' and '||map_filed_name||' >= '||min(map_filed_value)||' and '||map_filed_name||' <= '||max(map_filed_value)
    from b where map_filed_type = '05' group by map_filed_name;
      

  2.   

    declare
    v_sql varchar2(4000) := ' select * from a where 1 = 1 ';
    begin
    for i in (select ' and '||map_filed_name||' = ' ||map_filed_value v
             from b where map_filed_type = '01'
             union 
             select ' and '||map_filed_name||' in ('||wm_sys.wm_concat(map_filed_value)||' )' v
             from b where map_filed_type = '02' group by map_filed_name
             union
             select ' and '||map_filed_name||' >= '||min(map_filed_value)||' and '||map_filed_name||' <= '||max(map_filed_value) v
             from b where map_filed_type = '05' group by map_filed_name) loop
       v_sql := v_sql ||i.v;
    end loop;
    dbms_output.put_line(v_sql);
    end;
    /
      

  3.   

    补充一下~~B表中的条件是变化的(就是说这次条件中可能需要area_no作为条件限制  也许下次就没有这个条件限制了)。 因为B表中的数据都是通过网页上配置产生的。
      

  4.   

    2楼的大哥   这个是什么东东  wm_sys.wm_concat
      

  5.   

    我知道那个函数的用法了 但是我没法使用  需要sys赋权限给普通用户?
      

  6.   

    wm_sys.wm_concat是个加了密的存储过程的名称!
      

  7.   


    是wmsys.wm_concat
    一般不用加wmsys.也可以。10g以上的版本可以使用
    shiyiwan这个过程写得真棒
      

  8.   

    家里也没法测试,只有mysql测测简单的sql还可以。
    wmsys我写错了,呵呵。
      

  9.   

    但是B表的map_filed_type只有这三种01,02,05对不对,那就可以的
      

  10.   

    谢谢你了~~~对了,我B表中map_filed_name 可能不止这些的  每次的条件可能都不一样  B表中的数据都是通过用户在网页上设置而生成的  所以应该还需要更灵活点。
      

  11.   

    不是  B表中map_filed_type 这个字段有6种类型的  分别为01,02,03,04,05,06  分别代表用‘=’‘in’ '=' '=' '>= 和<=' 'like'
      

  12.   

    map_filed_name再多都可以的,关键是map_filed_type是不是只有这三种,如果是 那这个sql就可以
      

  13.   

    shiyiwan  太谢谢了~~让我叫一声大哥吧
      

  14.   


    还有点点小问题  就是当map_filed_type=‘03’ map_filed_name =‘pay_fee’(A表中为NUMBER类型) 的时候和当map_filed_type=‘03’ map_filed_name =‘stat_date’(在A表中此字段存储为DATE类型,但是在B表中的时候存放的为条件类型为NUMBER) 的时候这样写SQL就有点问题了 涉及到引号的使用的问题 。  这里就涉及到了2种类型  而且我并不知道到底传过来的是何种类型。因为假如为number和date类型的话查询语句的写法就不一样了  一个不需要用 '' date需要用''。  所以我就考虑了应该要用函数来解决,但是  这样又会出现一个问题  我无法判断哪个大哪个小 就是不知道何时用 >=  何时用 <=