E
E
enchikiben2011-01-25 12:24:14
MySQL
enchikiben, 2011-01-25 12:24:14

Selection - parent, ancestor, ancestor of ancestor, etc.

Good afternoon!

Immediately I apologize for the "pun" in the title and move on to the question.

There is a table:

id parent_id name
one 0 a
2 one b
3 one in
4 3 G


Whether it is possible to select the parent and all its ancestors and ancestors of ancestors as one request?

There can be any number of levels.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pavelbelousov, 2011-01-25
@pavelbelousov

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/.

T
Tapac, 2011-01-25
@Tapac

Read this article explainextended.com/2009/07/20/hierarchical-data-in-mysql-parents-and-children-in-one-query/

C
charon, 2011-01-26
@charon

in short, with your limitations and the chosen method of storing the tree, it is impossible. More details in the links in the posts above. It is necessary to slightly change the tree storage architecture.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question