现在一张表,有这么几个字段:id,name,description,field_name,field_desc,parentID.
现在因为需要做出一个树出来,所以需有写一条SQL语句,条件如下,查询出所有的父节点以及父节点以下的子节点,再好是能分两个查询语句,一条查询语句查询出所有的父节眯,一表查询语句查询出所有的子节点。谢谢。
[b]问题补充:[/b]
所有的节点都是一次性加载进来的。
oracle中树的查询是用的connect by prior start with 用法
http://www.cnblogs.com/leup/archive/2007/11/18/962943.html
http://china.zjq.blog.163.com/blog/static/34115397200952632226969/
http://kingapex.iteye.com/blog/233179
http://www.iteye.com/topic/287749
select * from table start with id = 1 connect by prior id = parentId
这是父节点。
select * from table start with id ='父节点Id' connect by id = prior parentId
这个是兄弟节点。
不知道是不是你想要的:
所有父节点:
select c.* from (select a.*,(select count(*) RN from Table b where b.parentID = a.id) LEAF from Table a) c where c.LEAF > 0
所有子节点:
select c.* from (select a.*,(select count(*) RN from Table b where b.parentID = a.id) LEAF from Table a where a.parentID is not null) c where c.LEAF = 0
其中条件where a.parentID is not null这个也可以不需要
另外树形数据表的查询经常会用CONNECT BY子句,具体用法可以百度/谷歌或在JAVAEYE上搜索一下,例子很多。