作者SeanBoog (施吉祥先生)
标题Re: [SQL ] select语法
时间Wed Jan 18 12:00:14 2017
※ 引述《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
或许你应该可以从树状查询下手比较好
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 211.21.158.127
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1484712017.A.B3B.html
※ 编辑: SeanBoog (211.21.158.127), 01/18/2017 12:04:01