说明:connect by 一般用来生成树状结果集,oracle中的select语句可以用START WITH...CONNECT BY PRIOR子句实现递归查询,connect by 是结构化查询中用到的
语法:
select ... from
where
start with
connect by
;
:过滤条件,用于对返回的所有记录进行过滤。
:查询结果重起始根结点的限定条件。
:连接条件
例:
select * from dbtest_area
select id,pid,rpad(' ',(level-1)*3)||name as name ,level
from dbtest_area
start with id =1
connect by prior id = pid
level表示的是每行记录的层数,根节点为1,直接子节点为2,子子节点为3
节点和分支的过虑
--过虑掉成华区
在这个查询中过虑掉结点,成华区的数据
select id,pid,rpad(' ',(level-1)*3)||name as name ,level
from dbtest_area where name <> '成华区'start with id =1 connect by prior id = pid
--过虑掉分结点下的一个子结点
--过虑掉青羊中学这个分结点,且把它下边的所有结点都过虑掉
select id,pid,rpad(' ',(level-1)*3)||name as name ,level
from dbtest_area start with id =1 connect by prior id = pid and name <> '青羊中学'
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15720542/viewspace-619822/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/15720542/viewspace-619822/