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