MySql Modified preorder树创建url

时间:2013-12-06 11:15:31

标签: php mysql database-design content-management-system seo

此问题基于我之前关于stackoverflow的问题。这次我尝试使用Modified Preorder Tree方法为pages表中的所有行生成url。

以下是MySql表:[注意:我已经为观众添加了'父'列,以便了解类别表中的父子关系。实际的修改前序列树方法不使用父列(父子关系)]

Category table

Modified Preorder Traversal Tree

Pages table

pages table

我正在尝试编写一个mysql查询,它将使用产生以下输出的类别表生成pages表中所有行的URL。

预期行:

/content/article/array/oops/classes
/content/article/array/oops/objects
/content/article/php/index
/content/article/php/quiz
/content/article/php/strings/strstr
/content/article/php/strings/str_substr
/content/blog/something1
/content/blog/something2
/content/blog/java/test-java
/content/blog/java/final-method
/content/about-us
/content/contact-us

以下是我在类别表上尝试引用Managing Hierarchical Data in MySQL的内容。下面的SQL查询检索类别表中标题'oops'的单个路径。

SELECT concat('/',group_concat(parent.title separator '/')) as url
FROM category AS node, category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.title = 'oops'
ORDER BY parent.lft;

1 个答案:

答案 0 :(得分:1)

这样的事情就足够了......

SELECT CONCAT_WS('/',x.path,y.page_name) full_path
  FROM
     ( SELECT node.id, GROUP_CONCAT(parent.title ORDER BY parent.lft SEPARATOR '/') path
         FROM category node
         JOIN category parent
           ON node.lft BETWEEN parent.lft AND parent.rgt
        GROUP 
           BY node.title
        ORDER 
           BY node.lft
     ) x
  JOIN pages y
    ON y.category_id = x.id;