Re: [SQL ] select语法

楼主: SeanBoog (施吉祥先生)   2017-01-18 12:00:14
※ 引述《wins888 (海)》之铭言:
: 数据库名称:ORACLE
: 数据库版本:10g
: 内容/问题描述:
: select * from bmb_file where bmb01='A' and bmb03='B';
: select * from bmb_file where bmb01='B' and bmb03='C';
: select * from bmb_file where bmb01='C' and bmb03='D';
: 请问我该如何下一行查询语法直接用bmb01='A' 查询bmb03='D'?
: 感谢.
假设bmb01是父件, bmb03是子件
二阶表达
select b.bmb01,b.bmb03
from bmb_file a,bmb_file b
where a.bmb03=b.bmb01
and a.bmb01='A'
出来结果会是
BMB01 | BMB03
A01 | B01
A01 | B02
A01 | B03
A01 | B04
....
以此类推, 四阶会是
select d.bmb01,d.bmb03
from bmb_file a,bmb_file b,bmb_file c,bmb_file d
where a.bmb03=b.bmb01
and b.bmb03=c.bmb01
and c.bmb03=d.bmb01
and a.bmb01='A'
递回很直觉, 但效能须注意, 而且这也不是弹性的好方法
(假设今天需求不是4阶直查, 那SQL就需要调整了)
顺便补充一下树状查询
select level,bmb01,bmb03
from bmb_file
start with bmb01='A'
connect by prior bmb03= bmb01
或许你应该可以从树状查询下手比较好

Links booklink

Contact Us: admin [ a t ] ucptt.com