在重新开发已经有过的一个项目时,参考原本的sql,在一个存储过程中有用到 start with connect by,从网络找到下面资料。

Oracle 提供了start with connect by 语法结构可以实现递归查询。
 from test;
 DAY_NUMBER MSISDN
-------------------- ---------- --------------------
200803                        1 13800
200803                        3 13800
200803                        2 13800
200803                        2 13801
200803                        4 13804
200803                        5 13804
200803                        7 13804
200803                        8 13804
200803                        6 13802
200803                        6 13801
200803                        7 13801
200803                        8 13801

SQL>
SQL> select * from test
  2       start with day_number=1
  3       connect by  prior day_number=day_number-1 and prior msisdn= msisdn
  4      ;
 DAY_NUMBER MSISDN
-------------------- ---------- --------------------
200803                        1 13800
200803                        2 13800
200803                        3 13800


 connect by 语法结构
 如上面说看到的 例子, 其语法结构为  start with condition  connect by  condition (含 prior 关键字)
start with conditon 给出的seed 数据的范围, connect by  后面给出了递归查询的条件,prior 关键字表示父数据,prior 条件表示子数据需要满足父数据的什么条件。

start with day_number=1
     connect by  prior day_number=day_number-1 and prior msisdn= msisdn

 

posted on 2016-11-09 09:33  java频道  阅读(2174)  评论(0编辑  收藏  举报