How to generate a tree view from this result set based on Tree Traversal Algorithm?

后端 未结 2 1868
感情败类
感情败类 2020-12-19 06:22

I have this table:

CREATE TABLE `categories` (
  `id` int(11) NOT NULL auto_increment,
  `category_id` int(11) default NULL,
  `root_id` int(11) default NULL         


        
2条回答
  •  感动是毒
    2020-12-19 06:55

    I got it.

    All you need to do is set root_id to the top parents too, so that you can ORDER BY correctly.

    With the query bellow I can have separeted trees, and uptade only the tree that I'm working on:

    SELECT c . * , count( p.id ) AS depth
    FROM `categories` c
    CROSS JOIN categories p
    WHERE (
    c.lft
    BETWEEN p.lft
    AND p.rht
    )
    AND c.root_id = p.root_id
    GROUP BY c.id
    ORDER BY c.root_id, c.lft
    

提交回复
热议问题