select level,A.*
from A
connect by prior B.PARENTID=A.ID
starts with B.PARENTID=?;

解决方案 »

  1.   

    需要的最终结果应该是如下的字段集合列表:
    ID,PARENTID,F1,F2...
    假定ID,PARENTID都是数值型,并且大类的PARENTID为-1,则结果集应该是:1,-1,abcd,abcd
    2,-1,abcd,abcd
    ...
      

  2.   

    以上源于假定表A有记录:
    1,-1
    2,-1
    3,1
    4,1
    ...
    表B有记录:
    3,abcd,abcd
    4,abcd,abcd
    ...
      

  3.   

    select a.*,b.f1,b,f2 from tabA a,tabB b where a.id=b.id不知道是不是你要的结果
      

  4.   

    楼上的查询只是一个简单的表连接,当然不是要求的检索结果。考虑了一个下午,有如下sql语句实现:
    select a.id,a.parentid,b.f1,b.f2
    from (select id,parentid from A where parentid = -1) a,
         (select id,f1,f2 from B) b
    where a.id in (select c.parentid from A c start with c.id = b.id
                   connect by prior c.parentid = c.id)
                   )
    感觉应该就是这样了,可是执行效率实在有些不满意,现在A表不过数百条记录,B表也不过5万条记录,将来的查询恐怕效率更糟糕,呵呵。
    哪位大虾还有好的提议没有?
      

  5.   

    如果表A只有两级关系,就是说任何id只对应一个上级,用一条语句可以查
    select a.id, a.parentid, b.f1, b.f2
    from a, b
    where a.id = b.id and a.parentid = -1;
    如果表A有多级关系,建议用游标做,效率会高的多,也容易维护
      

  6.   

    楼上的查询返回的结果集应该为空吧。:(首先a.id = b.id,建立的连接关系,将大类排除出去了;
    而a.parentid = -1,则由于小类的parentid均不是-1,因此将小类也排除了。
    实际上原本的查询要求是想由小类的包含情况替换为大类的包含情况。我想我没有分析错楼上的查询结果吧。:(呵呵,还有游标没法用了,我是想将这条查询生成视图。
      

  7.   

    select a.id, a.parentid, b.f1, b.f2
    from a, b
    where a.id = b.id(+)
      

  8.   

    try this and good luck! SELECT A1.id,A1.parentid, B.F1,B.F2, <other fields go here>
    FROM (select * from A 
          connect by prior parentid=id
          start with parentid=-1) A1,B
    WHERE A1.ID=B.ID
          AND B.F1=<your condition>
          AND B.F2=<your condition>
      

  9.   

    有点类似BOM中的部件和具体组成。