select PC.NAME,(case when instr(PC.sourcecode,tb_cp.sourcekpiid) > 0 then tb_cp.capesname else null end) from cp_kpi_info_fy tb_cp, cp_om_kpi_info_fy PCtb_cp.sourcekpiid这个字段里包含1个或者几个PC.sourcecode字段的值。我现在想列出PC表里NAME字段的所有值,如果PC表里的sourcecode被包含在tb_cp.sourcekpiid里,那么就列出tb_cp的capesname字段。
但是我用上面那个查询出PC表里几十倍的值,我想是因为2个表间没加左连接,但是2个表就我说的这个被包含的关系,我怎么加左连接呢?

解决方案 »

  1.   

    select * from a, b
     where instr(',' || b.字段 || ',', 
                 ',' || a.字段 || ',' ) > 0;
      

  2.   

    with a as
    (select 'a123' aa from dual)
    ,b as
    (select 'a123,b456' bb from dual
    union all
    select 'c789,d123' from dual
    union all
    select 'a123,dadaf' from dual
    )select * from a,b where  bb like '%'||aa||'%'--result:a123    a123,b456
    a123    a123,dadaf
      

  3.   

    select pc.name ,tb_cp.capesname
    from cp_om_kpi_info_fy PC left join cp_kpi_info_fy tb_cp
    on instr(PC.sourcecode,tb_cp.sourcekpiid) > 0
      

  4.   

    select t3.capesname from cp_kpi_info_fy t3, cp_om_kpi_info_fy t4 where instr(t4.sourcecode,t3.sourcekpiid) > 0这样做,只能查到t3.sourcekpiid里包含t4.sourcecode的记录。我是想列出所有t4的记录,然后把t3里包含t4的列出名字来,不包含的显示为空
      

  5.   

    select t3.capesname from cp_kpi_info_fy t3 right join cp_om_kpi_info_fy t4 where instr(t4.sourcecode,t3.sourcekpiid) > 0
      

  6.   

    3楼这个把每行的值都写到sql里了,我的表里有200多行,我不可能这样写呀!其它人的回复,我都试了,多出来几百条数据。
    a表里有270行,我想保留a表所有值。b表符合条件的列出。
      

  7.   


    3楼的写法是因为你没给数据,你直接把它的select后面改写就完了。
      

  8.   

    都是高手,我只知道左联接是left join,三楼应该是没错的呀。
      

  9.   


    你左边的表和右边的表使用instr判断后不是1对1的关系,而是1对多的关系就会得到700多条,把on后面的条件完善成1对1关系就不会多了。
      

  10.   

    楼主,如你描述:“如果PC表里的sourcecode被包含在tb_cp.sourcekpiid里,那么就列出tb_cp的capesname字段。”
    你这里说PC表里的sourcecode被包含在tb_cp.sourcekpiid里,那就是说tb_cp.sourcekpiid值的范围比PC表里的sourcecode值大喽,比如tb_cp.sourcekpiid = aaaa111,pc.sourcecode = aaa,这样PC表里的sourcecode才算是被包含在tb_cp.sourcekpiid里,不知道楼主我这样理解对吗??
    如果是这样的,那楼主你的instr方法就用反了。
      

  11.   

    没错,instr里的参数用反了。请看我另一个页面,里面的描述很具体http://topic.csdn.net/u/20111101/10/f68794c6-7a7c-4afd-bd38-5c97d602ecfa.html?seed=185650643&r=76271783#r_76271783