2
I am setting up a hierarchy and everything is working correctly, I just have a small problem, in the ordering of the data.
Follows the structure of the table. (as basic as possible)
create table hierarquia (
`id` int(11) NOT NULL AUTO_INCREMENT,
`hierarquia_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
and the data are these:
insert into hierarquia values
(1, 0),
(2, 0),
(3, 1),
(4, 1),
(5, 0),
(6, 3);
When I do my query select * from hierarquia order by hierarquia_id, id
does not return the data in the order I need.
ID | HIERARQUIA_ID
1 | 0
2 | 0
5 | 0
3 | 1
4 | 1
6 | 3
It would need to return the data more consistently ordered by their hierarchies, and it would have to return the data like this:
ID | HIERARQUIA_ID
1 | 0
3 | 1
6 | 3
4 | 1
2 | 0
5 | 0
First the first hierarchy without sub-hierarchy, the next being the sub-hierarchy of the first and consecutively.
There is this possibility in the mysql ?
Man, relational databases are no good with tree structures like yours. You want to return the list of tree nodes when traversed in pre-order. Mysql is pretty spartan for handling spatial data.
– Henrique Barcelos
@Henriquebarcelos the bank itself runs well, my only problem even though it is in this ordination. I even understand what you mean, but I’m trying, because unfortunately I have no way to change this structure, very much to a noSql, at least not now. But Valew for the return.
– Marcelo Diniz
So there is no direct way to do this. The best way to do it is by using the programming language in the application that accesses this data.
– Henrique Barcelos
There are some "technical adjustments" that you find here and that can give a light to you solve your problem.
– Henrique Barcelos
I’ll take a look, and as I said, I might do it on the schedule anyway. Valew
– Marcelo Diniz