查询基本结构:
select … from table_name
start with 条件1
connect by 条件2
1、建测试用表
1 create tabletest_prior(2 ids number,3 son varchar2(200),4 father varchar2(200)5 );
并插入数据
start with指定树的根(即父节点)实际上你还可以指定多个根的,比如 father in (‘爷爷‘, ‘爸爸‘)
而connect by prior son = father相当于表名在递归的过程中,查找到的树中其它节点接着又作为根结点。然后继续递归(prior在的一侧表示该侧为子节点)
为了便于理解可以可以connect by prior那一行当作多余的,只记住要查询的列名放前面,根列名放等号后面就行,这样只要指定树的根结点就比较好理解了。
1 selectson2 fromtest_prior3 start with father = ‘爷爷‘
4 connect by prior son =father;5
6 select distinctson7 fromtest_prior8 start with father in( ‘爷爷‘ , ‘爸爸‘ )--指定多个根会出现重复的数据
9 connect by prior son = father;
2、START WITH 可以省略,比如
1 select