嵌套集类别,可见性

时间:2014-07-11 23:30:26

标签: mysql sql nested nested-sets

我有两个表页面和pages_to_menu,第二个保存菜单的嵌套数据,它们看起来像这样:

 pages : ID | Title | Published
        ----------------------
         1     P1      1
         2     P2      0
         3     P2.1    1
         4     P2.2    1

 pages_to_menu:
         ID | lft | rgt | level | page_id | parent_id
         ---------------------------------------------
          1    1     10     0        0         0
          2    2     3      1        1         1
          3    4     9      1        2         1
          4    5     6      2        3         3
          5    7     8      2        4         3

所以当我执行这个查询时,我能够得到整棵树:

SELECT node.level, node.page_id, page.title
FROM 
        pages_to_menu AS node
        Left Join pages as page on page.id = node.page_id
        ,
        pages_to_menu AS parent
WHERE 
        node.lft BETWEEN parent.lft AND parent.rgt  

GROUP BY node.id
ORDER BY node.lft 

显示:

  level | page_id | Title
  ------------------------
   0          0       Null
   1          1       P1
   1          2       P2
   2          3       P2.1
   2          4       P2.2

问题是P2被发布= 0所以P2及其所有孩子,P2.1,P2.2都必须被隐藏

正确的输出必须是

  level | page_id | Title
  ------------------------
   0          0       Null
   1          1       P1

我试过

SELECT node.level, node.page_id, page.title
FROM 
        pages_to_menu AS node 
        ,
        pages_to_menu AS parent
        INNER Join pages as page on page.id = parent.page_id  AND page.published = 1
WHERE 
        node.lft BETWEEN parent.lft AND parent.rgt  

GROUP BY node.id
ORDER BY node.lft 

但这仅影响父节点

  level | page_id | Title
  ------------------------
   0          0       Null
   1          1       P1 
   2          3       P2.1
   2          4       P2.2

P2.1和P2.2仍在那里。

对不起,如果我的英语有点不好,感谢您提前获取任何帮助!

0 个答案:

没有答案