此问题基于我之前关于stackoverflow的问题。这次我尝试使用Modified Preorder Tree方法为pages表中的所有行生成url。
以下是MySql表:[注意:我已经为观众添加了'父'列,以便了解类别表中的父子关系。实际的修改前序列树方法不使用父列(父子关系)]
我正在尝试编写一个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;
答案 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;