java - tree形结构的Nested Set 数据如何转成Adjacency List的
伊谢尔伦
伊谢尔伦 2017-04-18 10:43:08
0
1
418
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
伊谢尔伦

I thought it was simple before and needed one more layer of subqueries. The modification is as follows:

SELECT node.name, parent.id AS pid
FROM `nested` as node
  LEFT OUTER JOIN `nested` AS parent
    ON parent.`left` = (
      SELECT MAX(parents.`left`)
      FROM nested AS parents
      WHERE node.`left` > parents.`left` AND node.`left` < parents.`right`
    )
ORDER BY node.id;

Refer to the second example in the "Getting the Whole Tree" section of https://segmentfault.com/a/11... that I translated and compiled before. The main modification is to change (COUNT(parent.name) - 1) AS depth 改成 parent.id to get a form similar to the adjacency list. .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!