简体   繁体   English

从数据库加载树

[英]Loading Tree from Database

I have some Categories, but some of them have sub-categories. 我有一些类别,但其中一些具有子类别。 So I have a Tree. 所以我有一棵树。

I created a table which called Categories . 我创建了一个名为Categories的表。 The id field is the primary key the other field is PARENTID which is foreign key referenced by the id ( To make it hierarchical). id字段是主键,另一个字段是PARENTID ,这是id引用的外键(使其成为分层结构)。 you can have a view of data here 你可以在这里查看数据

在此处输入图片说明

I am going to specify a certain id and the query gives me the id 's of that category and all sub-categories . 我将指定一个特定的ID ,查询将为我提供该类别和所有子类别ID I have red this and this one ,but none of them are not useful. 我已经把这个这个红色了,但是它们都不是没有用的。

one more point: I am not asking you to write the query for me. 还有一点:我不是要您为我编写查询。 I am just looking for some hints 我只是在寻找一些提示

I'll suggest two options. 我建议两个选择。

1) specify your select to include sub cursors, 1)指定您的选择以包括子游标,

select column1, cursor(select * form table2) as cursorOfTable2 from table1

and then in your middle tier code, you can reference the sub cursor, the implementation varies depending on what your middle tier is (j2ee, .net, ...) 然后在您的中间层代码中,您可以引用子游标,具体实现取决于中间层是什么(j2ee,.net,...)

2) specify the parent child relationship within your select 2)在您选择的范围内指定父子关系

select a.id as parentId, b.id as child_id, from table1 a, table2 b
where a.id = b.parentId

And then again in your middle tier code you can create the parent child data structure with this data. 然后再次在中间层代码中,可以使用此数据创建父子数据结构。

hope that makes sense. 希望这是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM