problem with hierarchical database model?

前端 未结 1 621
梦毁少年i
梦毁少年i 2021-01-28 01:49

I got a problem with my db design, I am developing a forum in which you can add topcis and then subtopics inside that forum and so on but in that subforum It must show the answe

相关标签:
1条回答
  • 2021-01-28 02:20

    I had to do this in MySQL recently, MySQL unfortunately does not offer anything useful for processing hierarchical data.

    I used an extra varchar column called path where I would store the 'path' leading to current item. E.g. a row with an id=127 which is a child of id=120 which is a child of id=15 would have its path='/15/120/127'.

    You need to take care of this on your own when inserting data.

    Extracting all rows that are direct or non-direct children of a row with id=15 would then look like:

    SELECT * FROM table WHERE
      path LIKE '%/15/%'
    

    I can also see that this is a rather much discussed topic here on Stackoverflow. Look on the right in Related column next to this question.

    0 讨论(0)
提交回复
热议问题