各位高手,小弟现在碰到一个问题,场景描述如下
现在有一张表 model  ,记录了 模型与其符合条件的关系,模型与条件是多对多的关系,
create table model (model_id varchar2(10),condition_id varchar2(2),...)
例如:
model_id       condition_id
-----------    ------------
0000000001       1
0000000001       3
0000000002       2
0000000002       3
0000000003       4
........
还有一张表 rule ,记录了 规则与其包含条件的关系,规则与条件关系也是多对多的
create table rule (rule_id varchar2(2),condition_id varchar2(2),...)
例如:
rule_id         condition_id
-----------     ------------
   01               1
   01               4
   02               3
   03               2
   03               3
......当 模型符合的条件的集合 可以包含 规则所包含条件的集合 时,则认为 模型满足规则 最终将插入结果表 result
create table result (model_id varchar2(10),rule_id varchar2(2),...) 
像这样的SQL语句,除了用两层循环 取出 每个模型的条件 与 每个规则的条件 比对以外,有没有什么更高效的SQL可以解决这个问题,希望各位高手能给予解答,谢谢!

解决方案 »

  1.   


    --LZ,看下这个结果是不是你要的
    SQL> with model(model_id,condition_id) as(
      2  select '0000000001',1 from dual
      3  union all select '0000000001',3 from dual
      4  union all select '0000000002',2 from dual
      5  union all select '0000000002',3 from dual
      6  union all select '0000000003',4 from dual
      7  ),
      8  rule(rule_id,condition_id) as(
      9  select '01',1 from dual
     10  union all select '01',4 from dual
     11  union all select '02',3 from dual
     12  union all select '03',2 from dual
     13  union all select '03',3 from dual
     14  )
     15  select model_id,rule_id from
     16  (select model_id,wm_concat(condition_id) c from model
     17   group by model_id) t1,
     18  (select rule_id,wm_concat(condition_id) c from rule
     19   group by rule_id) t2
     20  where t1.c=t2.c;MODEL_ID             RULE                                                       
    -------------------- ----                                                       
    0000000002           03   
      

  2.   

    晕,包含的你就不用相等了,直接两个字段instr不就可以了

    select * from dual where instr('A,B,C','A,B')>0;
      

  3.   

    但是 如果 模型 1 符合条件 1 ,2 ,4
    而 规则 1 包含条件 为 1,4时,也认为 模型1 符合 规则1,但 instr 是 为0的,这种情况就没法处理了