Domanda

Say I have a table with wiki pages. These belong to a certain wiki shown by the wiki_id. There are different wikis. So the table looks like this:

ID:  WIKI_ID:  TITLE:    PARENT_ID:
1    3         Page1     Null
2    3         Page2     1
3    3         Page3     1
4    3         Page4     3
5    3         Page5     4
6    4         Page1     Null
7    4         Page2     6

Now my main target is to copy Page3 and all children pages to another wiki. And here goes my problem as I am using a MySQL database I cannot use the with-clause to select them.

I found a way to select all the needed pages by setting a tag in the title field. But I cannot just insert them the way they are as I need to adjust the parent_id. So my table should look like.

ID:  WIKI_ID:  TITLE:    PARENT_ID:
1    3         Page1     Null
2    3         Page2     1
3    3         Page3     1
4    3         Page4     3
5    3         Page5     4
6    4         Page1     Null
7    4         Page2     6
8    4         Page3     6
9    4         Page4     8
10   4         Page5     9

So what I thought about is something like this:

  • I first select just the Page3 from wiki with the wiki_id: 3

  • Then I select the startpage(Page1) of the new project to get the right parent_id. This page I get by searching the page from a certain project with parent_id equal null as page3 will be always child of the startpage in every wiki

  • After this I would select all the pages from the table which got the parent_id equal the id of the page3 from wiki with the wiki_id: 3 and select the new page3 from wiki with the wiki_id: 4 to get the right parent_id again and insert them into the table

I would repeat the last step over and over if I knew the depth and this exactly is my problem. The depth may vary from time to time. Hope you know any solution to find the max depth of an element in the tree. I also am open for other solutions to solve this whole problem.

È stato utile?

Soluzione

Solved it in another way. I just inserted the new pages with the original parent_id. Afterwards I joined them over the title column to get the original and the new ID in the same row. And made update for each row to update the pages of the new pages by setting the parent_id to the new ID of the page

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top