NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.
All the Perl that's Practical to Extract and Report
Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.
FIELD? (Score:1)
Too lazy to test it by would something like
ORDER BY FIELD( id, 5,7,1,48)
do the job?
Re: (Score:2)
I think that's MySQL specific. The very simple solution is much easier.
Re: (Score:1)
Ah...
ORDER BY CHAR_LENGTH( path )
?
Re: (Score:2)
That's pretty much it. I responded to Ricardo with the solution and a caveat [perl.org].
too easy? (Score:1)
This seems obvious?
ORDER BY id=5, id=7, id=1, id=48
rjbs
Re: (Score:2)
Is that SQL standard? The solution I've gone with is actually much simpler.
Re: (Score:1)
I have never seen a SQL where that didn't work. I am also interested to see what could be much simpler than that. :)
rjbs
Re: (Score:2)
OK, a few people on Twitter have struck out on this and Abigail guessed something similar to what you and Adrian guessed.
The solution is:
Turns out it's a deterministic emergent property of materialized paths. There is one caveat I realized, though. If you have multiple trees and you want to see the root nodes, you can do this:
However, some people omit the ID from the path. So you'd have this (in particular, note the path for the 5 node):
Re: (Score:1)
Seems problematic:
1.23.45.67
1.2.3.4.5
Perhaps you could count dots.
rjbs
Re: (Score:2)
No, you can guarantee that conflicting paths won't exist as you walk up the tree.
Re: (Score:1)
_"Seems problematic:"_
Nah - the problem is selecting all the parents of a child in order. The length of the path is guaranteed to increase.
Re: (Score:1)
Re: (Score:1)
Re: (Score:2)
With the caveat above, yes, I think it might be.
Have you looked at nested sets? (Score:1)
Depending on the size of the tree, I've found that the nested sets representation is very nice for queries.
The query that you need is given as an example in this page:
http://dev.mysql.com/tech-resources/articles/hierarchical-data.html [mysql.com]
(Although this is a mysql.com site, there is nothing MySQL specific with the solution)
The problem with nested sets is updates: they might change a large number of records - worst case, inserting a new node at the left-most place will update all the rows.
But smaller trees, they
life is short