[英]How to fetch children tree of given parent in B-tree
Suppose I have table called tree
with id
, and parent-id
and data are b-tree假设我有一个名为
tree
表, id
,并且parent-id
和 data 是b-tree
1
2 3
4 5 6 7 8 9
etc等等
all these are store in tree table like所有这些都存储在树表中
1 - null
2 - 1
3 - 1
4 - 2
5 - 2
6 - 2
7 - 3
8 - 3
9 - 3
etc等等
so please write Query to fetch child tree of 2所以请编写查询来获取 2 的子树
output should be like输出应该像
4 - 2
5 - 2
6 - 2
xx - 4
etc..
Select id, parent_id from tablename where parent_id = 2. Perhaps you need bottom level ones?选择 id, parent_id from tablename where parent_id = 2. 也许你需要底层的? Then you need to left join the table with itself and only return the rows that don't have children.
然后,您需要将表与自身左连接,并仅返回没有子项的行。
Edit: if the maximum number of hierarchy levels is high or unknown, you should use hierarchyid: https://msdn.microsoft.com/en-us/library/bb677213.aspx编辑:如果最大层次级别数很高或未知,则应使用hierarchyid: https : //msdn.microsoft.com/en-us/library/bb677213.aspx
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.