相关表单如下所示:
ID NAME
1 张三
2 李四
3 王五XUH SUBJECT
1,2         语文
2,3         数学
1,2,3 物理
1,3         英语
1              生物想实现如下所示:
XUH NAME           SUBJECT
1,2        张三,李四   语文
2,3        李四,王五   数学
1,2,3      张三,李四,王五 物理
1,3        张三,王五   英语
1             张三                   生物麻烦指点一下,谢谢!

解决方案 »

  1.   

    select xuh,(select wm_concat(name) from t1 where instr(xuh,id)>0) as name,subject from t2
      

  2.   

    select xuh,wm_concat(name) name, subject
    from (
    select t2.xuh,t1.id,t1.name,t2.subject
    from t2
    join t1
    where instr(t2.xuh,t1.id) > 0
    order by t1.id) tt
    group by xuh,subject;
      

  3.   

    我的Oracle数据库版本不支持wm_concat这个函数,另外这个instr不行吧?现在我给的示例是1,2,3,可以这样处理。假如是1,12,123这样的序号的话,用instr就有问题了。
      

  4.   

    也不会有问题,wm_contact没有的话可以下载一个包,实在不行只能用行列转换,然后拼接字段咯
      

  5.   

    如果是9i以下,建议写个函数处理好一点。
    如果10G以上,用这个不会有问题了:
    select xuh,
    (select wm_concat(name) from t1 
     where id in 
     (select case when instr(xuh,',')=0 then xuh else regexp_substr(xuh,'[^,]',1,level) end  
      from dual connect by level<=length(xuh)-length(replace(xuh,',',''))+1)) 
    as name,subject 
    from t2
      

  6.   

    设计有问题,简单的问题复杂化。不过当作业做还是可以的:
    select SUBSTR(XUHS,
              case when POS=1 then 1
                   else INSTR(XUHS || ',',',',1,POS-1)+1
              end,
              case when POS=1 then INSTR(XUHS || ',',',',1,POS)-1
                   else INSTR(XUHS || ',',',',1,POS)-INSTR(XUHS || ',',',',1,POS-1)-1
              end
           ) XUH,SUBJECT
      from (select XUH xuhs,length(XUH)-length(replace(XUH,',',''))+1 NUMS,SUBJECT from T2) a, 
           (select rownum POS from T1) B
     where B.POS<=a.NUMS
     order by XUH,SUBJECT;