P
P
Pavel K2015-04-08 21:17:48
MySQL
Pavel K, 2015-04-08 21:17:48

tree menu. How to choose first ancestors, then descendants?

Good evening!
There is a table for a tree menu
for example

id, parentid, title

Accordingly, through parentid it is set to which ancestor this descendant belongs.
When I make a request to the database, it gives everything mixed up (if sorted in ascending order of IDs)
and when I try to display such a tree-like menu in the program, of course, some descendants do not have ancestors (because in the selection it can be after the descendants)
here as since the main difficulty is how to make a request correctly?
Those. What would be all the ancestors first, then all the descendants (or an ancestor - a descendant, as it turns out)?
PS In the program, I can first create an array, and then go over it and put everything in its place,
but I want to avoid this and get a ready-made sample.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2015-04-08
@PavelK

Change to the Nested Set structure . In this case, the selection in the desired order is done by sorting by the left key.

X
xmoonlight, 2015-04-08
@xmoonlight

stored procedures. Cycles

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question