Sample — parent, ancestor, ancestor ancestor, etc


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
8 views
Good day!

Immediately apologize for the "pun" in the title and proceed to the question.

There is a table:

idparent_idname
10a
21b
31in
43g


Is it possible as a single query to select parent and all its ancestors and ancestors ' ancestors?

Levels can be any number.
by | 8 views

4 Answers

0 like 0 dislike
You need to use recursive queries, but MySQL does not support them, as far as I know. Perhaps this article will help You: habrahabr.ru/blogs/sql/43955/.
by
0 like 0 dislike
by
0 like 0 dislike
I would advise you to enter a new field for the denormalization of the table. For example, the path field containing the full path from the root of parenta to the current element. For example, to record ID=4 will look like this: "0;1;3". In this case, if you want to get all childrenof element ID=42 will need to obtain the path of the element 42, and then do like '$pathTo42%'. In fact, to parentof ID=42 — like '%;42'
Perhaps attack the idea and not like it slow, they say. But mudkats with recursive samples is not necessary, and the recursion will load server much stronger.
\r
You can go the other way and to every record store and the nesting level will be immediately evident how many requests to perform.
In General, gehlenite about the methods of storage of trees by this thread lots of information.
by
0 like 0 dislike
in short, when your limitations and the chosen method of storage of a tree — impossible. Read more in the links in the posts above. You need to change the storage architecture of the tree.
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
3 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
27,842 users