start with connect by prior
select ... from tablename start with cond1
connect by cond2
where cond3;
簡單說來是將一個樹狀結構儲存在一張表裡,比如一個表中存在兩個欄位:
id,parentid那麼透過表示每一條記錄的parent是誰,就可以形成一個樹狀結構。
用上述語法的查詢可以取得這棵樹的所有記錄。
其中COND1是根結點的限定語句,當然可以放寬限定條件,以取得多個根結點,實際就是多棵樹。
COND2是連線條件,其中用PRIOR表示上一條記錄,比如 CONNECT BY PRIOR ID=PRAENTID就是說上一條記錄的ID是本條記錄的PRAENTID,即本記錄的父親是上一條記錄。
COND3是過濾條件,用於對返回的所有記錄進行過濾。
對於oracle進行簡單樹查詢(遞迴查詢)
DEPTID |
PAREDEPTID |
NAME |
NUMBER |
NUMBER |
CHAR (40 Byte) |
部門id |
父部門id(所屬部門id) |
部門名稱 |
透過子節點向根節點追朔.
Sql程式碼
select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid
透過根節點遍歷子節點.
Sql程式碼
Select * from persons.dept start with deptid=0 connect by prior deptid=partdeptid
可透過level 關鍵字查詢所在層次.
Sql程式碼
- select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid
再次複習一下:start with ...connect by 的用法, start with 後面所跟的就是就是遞迴的種子。
遞迴的種子也就是遞迴開始的地方 connect by 後面的"prior" 如果預設:則只能查詢到符合條件的起始行,並不進行遞迴查詢;
connect by prior 後面所放的欄位是有關係的,它指明瞭查詢的方向。
--轉載的部分,感覺寫的還不錯 http://blog.itpub.net/29564184/viewspace-1156956/來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29674916/viewspace-2133565/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 淺析REGEXP_SUBSTR,PRIOR,CONNECT BY
- Oracle start with connect by PostgreSQL recursive cteOracleSQL
- [轉載] Oracle:start with...connect by子句的用法Oracle
- Oracle優化案例-定位start with connect by遞迴死迴圈資料(二十二)Oracle優化遞迴
- oracle樹中prior的用法Oracle
- 影像分割 | Context Prior CPNet | CVPR2020Context
- start uniappAPP
- P3Depth: Monocular Depth Estimation with a Piecewise Planarity Prior3DMono
- Laravel connect oracleLaravelOracle
- database的connectDatabase
- rac中 crsctl start/stop crs and crsctl start/stop cluster 區別
- 3.1.5.6 Forcing an Instance to Start
- 開發springboot startSpring Boot
- INBOUND_CONNECT_TIMEOUT與SQLNET.INBOUND_CONNECT_TIMEOUT小結SQL
- CocoaAsyncSocket---Connect (下)
- QObject::connect()函式Object函式
- connect by層內排序排序
- connect your tunnel to CloudflareCloud
- AUTO START ORACLE ON LINUX(zt)OracleLinux
- where to start, from where the end
- [Kick Start] 2021 Round B
- INBOUND_CONNECT_TIMEOUT(zt)
- dojo.connect初解
- 17、Connect-the-dots(VulnHub)
- MQTT 協議 -- CONNECT & CONNACKMQQT協議
- Linux Use ODBC Connect OracleLinuxOracle
- Teamcenter 《POM has not start》處理
- Python connect zookeeper use the kazoo modulePython
- Host 'localhost' is not allowed to connect to this MySQL serverlocalhostMySqlServer
- SVN-Unable to connect to a repository at URL
- “Host ‘xxxx‘ is not allowed to connect to this MySQL server“MySqlServer
- Garmin Connect 活動分類
- ubuntu : xcb:could not connect to display.Ubuntu
- [20180416]connect by和nocycle.txt
- OpenID Connect Core 1.0 介紹
- HarmonyOS Connect認證測試
- Connect:一個更好的 gRPCRPC
- redis connect timeout問題排查Redis
- playwright 除錯: connect to exisisting broswer除錯ROS