[英]How to create a method in a binary tree structure to get the parent number of a node
[英]how create select query to get all the child node of any particular node in tree structure db table
我的树结构是
我创建的数据库表是
如何创建选择查询以获取树结构数据库表中任何特定节点的所有子节点。
例如,我通过了Superior_emp_id = 1,然后返回了{2,3,4,4,5,6,7}
select *
from employee
where superior_emp_id = @emp_id
union all
select *
from employee
where superior_emp_id in (select emp_id
from employee
where superior_emp_id = @emp_id)
好吧,您可以在此处使用嵌套查询和设置,
您可以尝试以下查询以获取根节点的子节点:
select *
from TABLE_NAME
where senior_emp_id in { select emp_id
from TABLE_NAME
where senior_emp_id = 1 || emp_id = 1 }
同样,如果要在父查询中使用子节点,则在嵌套查询中将2替换为1
让我知道它是否运作良好..
StringBuilder sql = new StringBuilder();
sql.append("SELECT emp_id AS id, ");
sql.append(" emp_name AS employeeName, ");
sql.append(" title AS title, ");
sql.append(" superior_emp_id AS parentId ");
sql.append(" FROM T_NWM_SRVC ");
sql.append(" WHERE superior_emp_id IS NOT NULL ");
sql.append(" AND superior_emp_id =");
sql.append(parentId);
传递parentId:1时,您将检索2,3。要获得2,3的子节点,您将不得不再次进行服务器调用(有点延迟加载)。 希望这是您想要的。
我认为您无法使用一个sql语句执行此操作。 我将尝试实现一个递归函数,如果有更多子代,则查询一组给定的ID并将其附加。 Prosa:
function List<Integer> getChilds(int[] ids)
{
List<Integer> returnValue = new ArrayList();
if(ids.length = 0)
return returnValue;
foreach int id : ids
{
//Build sql to get childs an execute it
int[] childidsfromsql = em.createQuery("...").getResultList();..
returnValue.addAll(getChilds(childidsfromsql))
}
return returnValue;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.