avatar
b*e
1
i have a question..
if i have a table which has two columns, parent_id and child_id
which is used to represent a tree structure.. ie.. there're many
levels.. and each node can have many children.. but only 1 parent.
so, records would be like (1, 2), (1, 3), (1, 4), (2, 5), (2, 6),
(3, 7), (5,8) etc etc
now how do you select all leaf level children of a known parent_id?
what about selecting all children that are <=x levels below the
parent_id?
i guess there must be a recursive way of doing this..
avatar
w*h
2
This is a typical BOM question.
SQL99 already supports recursive SQL.
You have a table: assembly(part, subpart,...)
CREATE RECURSIVE view all_subparts(Major, Minor) AS
SELECT PART SUBPART
FROM assembly
UNION
SELECT all.Major assb.SUBPART
FROM all_subparts all, assembly assb
WHERE all.minor = assb.PART
SELECT * FROM all_subparts

【在 b****e 的大作中提到】
: i have a question..
: if i have a table which has two columns, parent_id and child_id
: which is used to represent a tree structure.. ie.. there're many
: levels.. and each node can have many children.. but only 1 parent.
: so, records would be like (1, 2), (1, 3), (1, 4), (2, 5), (2, 6),
: (3, 7), (5,8) etc etc
: now how do you select all leaf level children of a known parent_id?
: what about selecting all children that are <=x levels below the
: parent_id?
: i guess there must be a recursive way of doing this..

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。