Generating Depth based tree from Hierarchical Data in MySQL (no CTEs)

前端 未结 4 515
误落风尘
误落风尘 2020-11-22 05:06

Hi For many days I have been working on this problem in MySQL, however I can not figure it out. Do any of you have suggestions?

Basically, I have a category table wi

4条回答
  •  感情败类
    2020-11-22 05:36

    The linear way:

    I am using a ugly function to create a tree in a simple string field.

    /              topic title
    /001           message 1
    /002           message 2
    /002/001       reply to message 2
    /002/001/001/  reply to reply
    /003           message 3
    etc...
    

    the table can be used to select all the rows in the tree order with a simple SQL Query:

    select * from morum_messages where m_topic=1234 order by m_linear asc

    INSERT is just select the parent linear (and children) and calculate the string as needed.

    select M_LINEAR FROM forum_messages WHERE m_topic = 1234 and M_LINEAR LIKE '{0}/___' ORDER BY M_LINEAR DESC limit 0,1  
    /* {0} - m_linear of the parent message*/
    

    DELETE is simple as delete the message, or delete by linear all replies of the parent one.

提交回复
热议问题