Tree structure solution in mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
tejasjoshi
Forum Newbie
Posts: 1
Joined: Wed Apr 22, 2009 8:55 am

Tree structure solution in mysql

Post by tejasjoshi »

I have one table which has three fields. MId Name and ParentId.

Suppose i have following table.

MId Name ParentId

1 a 0
2 a1 1
3 a2 1
4 a3 1
5 a4 1
6 a11 2
7 a12 2
8 a13 2
9 a111 6
10 a112 6
11 a113 6
12 a1111 9
13 a1112 9
14 a1113 9
15 a1114 9

Above table shows the MId(memberid), membername and parent of member.

I want to get all the child of particular member.

Suppose i have Mid=2 then we can get all the childs and subchilds of MId 2.

We can get result in following format.

Parent Child
a1 a11
a1 a12
a1 a13
a11 a111
a11 a112
a11 a113
a111 a1111
a111 a1112
a111 a1113
a111 a1114


I need solution of this question as soon as possible.
please help me.

Thanks in advance
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Tree structure solution in mysql

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
Post Reply