select a.*,b.f1,b,f2 from tabA a,tabB b where a.id=b.id不知道是不是你要的结果
楼上的查询只是一个简单的表连接,当然不是要求的检索结果。考虑了一个下午,有如下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万条记录,将来的查询恐怕效率更糟糕,呵呵。 哪位大虾还有好的提议没有?
如果表A只有两级关系,就是说任何id只对应一个上级,用一条语句可以查 select a.id, a.parentid, b.f1, b.f2 from a, b where a.id = b.id and a.parentid = -1; 如果表A有多级关系,建议用游标做,效率会高的多,也容易维护
select a.id, a.parentid, b.f1, b.f2 from a, b where a.id = b.id(+)
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>
ID,PARENTID,F1,F2...
假定ID,PARENTID都是数值型,并且大类的PARENTID为-1,则结果集应该是:1,-1,abcd,abcd
2,-1,abcd,abcd
...
1,-1
2,-1
3,1
4,1
...
表B有记录:
3,abcd,abcd
4,abcd,abcd
...
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万条记录,将来的查询恐怕效率更糟糕,呵呵。
哪位大虾还有好的提议没有?
select a.id, a.parentid, b.f1, b.f2
from a, b
where a.id = b.id and a.parentid = -1;
如果表A有多级关系,建议用游标做,效率会高的多,也容易维护
而a.parentid = -1,则由于小类的parentid均不是-1,因此将小类也排除了。
实际上原本的查询要求是想由小类的包含情况替换为大类的包含情况。我想我没有分析错楼上的查询结果吧。:(呵呵,还有游标没法用了,我是想将这条查询生成视图。
from a, b
where a.id = b.id(+)
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>