0

我有一组嵌套的组织,看起来像这样(节点是组织 ID):

嵌套的集合组织结构图

1234 是 4499 和 3322 的父级,但 9905 是 1234 的“附属”,而不是真正的父级或子级。

我相信leftedge,rightedge设置应该看起来像:

orgID | leftedge | rightedge
1234  |   1      |    6
4499  |   2      |    3
3322  |   4      |    5
9905  |   7      |    8

由于 9905 并不是真正的 1234 的子代,我不确定如何获得 1234 和 9905 之间的关系。我用来获取子代关系的 SQL 是:

SELECT ost.orgid, ost.leftedge, ost.rightedge
  FROM tbl_organisationsettree ost 
  JOIN tbl_organisationsettree AS child_ost ON child_ost.leftedge BETWEEN ost.leftedge AND ost.rightedge 
   AND child_ost.supersetid = ost.supersetid 
 WHERE 1 
   AND ost.leftedge > 1 
   AND ost.rightedge <6 
GROUP BY child_ost.ID 
HAVING COUNT(child_ost.ID) = 1
4

1 回答 1

0

查找不是 node 的后代或祖先的任何节点1234

SELECT ost.*     --- whatever from `org` table
FROM tbl_organisationsettree AS ost 
   JOIN tbl_organisationsettree AS param
      ON  param.leftedge NOT BETWEEN ost.leftedge 
                                 AND ost.rightedge
      AND ost.leftedge NOT BETWEEN param.leftedge 
                               AND param.rightedge
WHERE param.orgID = 1234 

--- if you only root nodes:
---
--- AND NOT EXISTS 
---     ( SELECT *
---       FROM tbl_organisationsettree AS parent
---       WHERE ost.leftedge > parent.leftedge 
---         AND ost.leftedge < parent.rightedge
---     )
;  
于 2012-07-09T15:15:50.850 回答