oracle-递归查询... start with ... connect by prior ...

--如果一个表,自带层级结构,类似树状,比如一个family表,有三个字段,name,mother,father,就可以显示一个家族的关系了--首先构建数据,只构建了一个三层结构


--如果此时我想要查询name为xxx的所有子节点包含name为xxx的数据,我们只看name和father
select * from family where name = 'xxx' 
union select * from family where father = 'xxx' 
union select * from family where father in (select name from family where father = 'xxx')
--这样写可以达到一个sql就ok了,但是但是,万一层级很深,那不是这个sql我们要写非常的长,而且每次都是重新查询,最后再进行并集处理,效率不高的
--所以才使用oracle提供好的关键字来进行查询,直接一次性搞定,不用去想oracle怎样弄的,肯定效率更高而且方便
select * from family start with name = 'xxx' connect by prior name = father;

猜你喜欢

转载自blog.csdn.net/blossomfzq/article/details/82895091