I did the following... I had a unique varchar(255) that held the tree string and used 2 digit base 36 ids... obviously you could extend the character set, but I figured 1296 children per leaf was enough. If you only need binary then one char is fine, 0-1 or something... you could probably encode something, but that may make queries difficult... obviously with 2 char codes you can only get 127 levels in a tree with a 255 char field.
I used a couple of self written functions to turn the codes to and from numbers:
To insert I would would first grab the max id within the group I wanted, something likesub de36 { my($a,$b) = (uc substr($_[0],-2,1),uc substr($_[0],-1,1)); $CHARS{$a}*36+$CHARS{$b}; } sub en36 { $CHARS[$_[0]/36].$CHARS[$_[0]%36]; }
matching those threads that were in the same parent....SELECT MAX(thread) FROM messages WHERE thread LIKE "${parent}__"
you can get all immediate children with stuff like
and you can get a history with something likeSELECT * FROM messages WHERE thread LIKE "${parent}__"
sorting is, in this case, super easy.SELECT * FROM messages WHERE thread = LEFT("$thread",LENGTH($thread))
I am using MySQL syntax but it should be clear enough...
You'll have to work out how to make it binary yourself, if you need it... not terribly difficult, select and compare, then add accordingly... table locking could probably help, too. :)
In reply to Re^3: Trees in SQL, adjacency lists and sorting.
by suaveant
in thread Trees in SQL, adjacency lists and sorting.
by BUU
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |