开发者

Nested query using while condition - sql

开发者 https://www.devze.com 2023-03-08 00:57 出处:网络
I need to do a ne开发者_开发知识库sted query on a single table. Each row is potentially the parent or child of another row.

I need to do a ne开发者_开发知识库sted query on a single table. Each row is potentially the parent or child of another row.

Is it possible to do this with a single select statement? Ive started using this statement but it only goes down one level.

select * from myTable where parent_id in 
   (select id from myTable where name = 'manager' )

This select however will only go down one level. If a row has multiple children they will be ignored. (In the table each row has an Id field , if a row has a parent then the parents Id value will be in the child's parent_Id field. )

If i could include a while loop in the SQL which would always check to see if the returned Id was a parent or not and if it was check and see if any other row was its child by checking the other rows parent_Id. However i m concerned this would take alot of cycles to eventually find all parent child relationships. Any suggestions? Thanks

using Oracle db


I think you are looking for a hierarchical query like this:

select * from mytable
connect by prior id = parent_id
start with name = 'Manager';

(A "nested table" is something else entirely.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号