Hi,
<b>Overview</b>
I am using the Nested Set Method to store hierarchical data in MySQL. The principle is that you can store each record with a left and right value, and all the subordinates of that node will have left and right values between those two values.
Here is an example of a table I have with the title of each record and their left and right values either side of them.
Anyway, say I notice that Cats is in the wrong category – instead of being under Farm it should be under Pet.
That would mean I would want my database to become
<b>Problem</b>
The problem is, I am having trouble working out the sort of queries I would perform.
To do this I:
1) Set the left value as the new parent (Pets) left value plus 1, and set the right value as this plus the width of the subtree (eg. 3+7).
2) Subtracting the change in the right value and left value (eg. left WAS 15 NOW 3) from all pages where the left value is between the OLD left and right of the subtree (eg. all the pages under the node Cats that I want to move (so Tabby, Tortoise Shell, Black).
I am happy with the above steps, but this is where I feel my problem is:-
3) For all pages with a left value greater than the OLD left value of the subtree, subtracting the change amount (from step 2) from their left.
4) For all pages with a right value greater than that of the old subtree’s left, but NOT with a left value greater than the subtree’s old left value, subtracting the change amount from their right value.
Obviously I wanted this to work whichever subtree I move around, eg. the whole of pets to under farm, or Dogs under Pigs>Pink.
I have spent such a long time thinking about this and getting nowhere so it would be great if anyone could provide a different view on the matter!
Thanks!
<b>Overview</b>
I am using the Nested Set Method to store hierarchical data in MySQL. The principle is that you can store each record with a left and right value, and all the subordinates of that node will have left and right values between those two values.
Here is an example of a table I have with the title of each record and their left and right values either side of them.
Code:
(1)Animals(24)
-(2)Pets(9)
--(3)Dogs(8)
---(4)Alsatian(5)
---(6)Labrador(7)
-(10)Farm(23)
--(11)Pigs(14)
---(12)Pink(13)
--(15)Cats(22)
---(16)Tabby(17)
---(18)Tortoise Shell(19)
---(20)Black(21)
That would mean I would want my database to become
Code:
(1)Animals(24)
-(2)Pets(17)
--(3)Cats(10)
---(4)Tabby(5)
---(6)Tortoise Shell(7)
---(8)Black(9)
--(11)Dogs(16)
---(12Alsatian(13)
---(14)Labrador(15)
-(18)Farm(23)
--(19)Pigs(20)
---(21)Pink(22)
The problem is, I am having trouble working out the sort of queries I would perform.
To do this I:
1) Set the left value as the new parent (Pets) left value plus 1, and set the right value as this plus the width of the subtree (eg. 3+7).
2) Subtracting the change in the right value and left value (eg. left WAS 15 NOW 3) from all pages where the left value is between the OLD left and right of the subtree (eg. all the pages under the node Cats that I want to move (so Tabby, Tortoise Shell, Black).
I am happy with the above steps, but this is where I feel my problem is:-
3) For all pages with a left value greater than the OLD left value of the subtree, subtracting the change amount (from step 2) from their left.
4) For all pages with a right value greater than that of the old subtree’s left, but NOT with a left value greater than the subtree’s old left value, subtracting the change amount from their right value.
Obviously I wanted this to work whichever subtree I move around, eg. the whole of pets to under farm, or Dogs under Pigs>Pink.
I have spent such a long time thinking about this and getting nowhere so it would be great if anyone could provide a different view on the matter!
Thanks!