如何从二叉树表中获取父子关系的数据?

时间:2016-06-25 07:59:24

标签: php mysql

如何从二叉树表中获取具有父子关系的数据而不进行递归?这是表结构。

id  member_id   member_parent_id                                                
1   1   9999    ROOT                                            
7   7   1   LEFT                                            
8   8   1   RIGHT                                           
9   9   7   LEFT                                            
10  10  7   RIGHT                                           
11  11  8   LEFT                                            
12  12  8   RIGHT                                           
13  13  9   LEFT                                            
14  14  9   RIGHT                                           
15  15  10  LEFT                                            
16  16  10  RIGHT                                           
17  17  11  LEFT                                            
18  18  11  RIGHT                                           
19  19  12  LEFT                                            
20  20  12  RIGHT                                           
21  21  13  LEFT                                            
22  22  13  RIGHT                                           
23  23  14  LEFT                                            
24  24  14  RIGHT                                           
25  25  15  LEFT                                            
26  26  15  RIGHT                                           
27  27  16  LEFT                                            
28  28  16  RIGHT                                           
29  29  17  LEFT                                            
30  30  17  RIGHT                                           
31  31  18  LEFT                                            
32  32  18  RIGHT                                           
33  33  19  LEFT                                            
34  34  19  RIGHT                                           
35  35  20  LEFT                                            
36  36  20  RIGHT                                           
37  37  21  LEFT                                            
38  38  21  RIGHT                                           
39  39  22  LEFT                                            
40  40  22  RIGHT                                           
41  41  23  LEFT                                            
42  42  23  RIGHT                                           
43  43  24  LEFT                                            
44  44  24  RIGHT                                           
45  45  25  LEFT                                            
46  46  25  RIGHT                                           
47  47  26  LEFT                                            
48  48  26  RIGHT                                           
49  49  27  LEFT                                            
50  50  27  RIGHT                                           
51  51  28  LEFT                                            
52  52  28  RIGHT                                           
53  53  29  LEFT                                            
54  54  29  RIGHT                                           
55  55  30  LEFT                                            
56  56  30  RIGHT                                           
57  57  31  LEFT                                            
58  58  31  RIGHT                                           
59  59  32  LEFT                                            
60  60  32  RIGHT                                           
61  61  33  LEFT                                            
62  62  33  RIGHT                                           
63  63  34  LEFT                                            
64  64  34  RIGHT                                           
65  65  35  LEFT                                            
66  66  35  RIGHT                                           
67  67  36  LEFT                                            
68  68  36  RIGHT                                           
69  69  37  LEFT                                            
70  70  69  LEFT                                            
71  71  70  LEFT                                            
72  72  71  LEFT                                            
73  73  72  LEFT                                            
74  74  73  LEFT                                            
75  75  74  LEFT                                            
76  76  74  RIGHT                                           
77  77  76  RIGHT                                           
78  78  77  LEFT                                            
79  79  77  RIGHT                                           
80  80  79  RIGHT                                           
81  81  80  RIGHT                                           
82  82  81  RIGHT                                           
83  83  82  RIGHT                                           
84  84  83  RIGHT                                           
85  85  84  RIGHT                                           
86  86  85  RIGHT                                           
87  87  86  RIGHT                                           
88  88  87  RIGHT                                           
89  89  88  RIGHT                                           
90  90  88  LEFT                                            
91  91  75  LEFT                                            
92  92  91  LEFT                                            
93  93  92  LEFT                                            
94  94  93  LEFT                                            
95  95  94  LEFT                                            
96  96  95  LEFT                                            
97  97  96  RIGHT                                           
98  98  97  LEFT                                            
99  99  98  LEFT                                            
100 100 99  LEFT                                            
101 101 100 LEFT                                            
102 102 101 LEFT                                            
103 103 102 LEFT                                            
104 104 103 LEFT                                            
105 105 104 LEFT                                            
106 106 105 LEFT                                            
107 107 105 RIGHT                                           
108 108 107 RIGHT                                           
109 109 106 LEFT                                            
110 110 109 LEFT                                            
111 111 110 LEFT                                            
112 112 111 LEFT                                            
113 113 111 RIGHT                                           
114 114 68  RIGHT                                           
115 115 114 RIGHT                                           
116 116 115 LEFT        

1 个答案:

答案 0 :(得分:0)

您需要使用简单的JOIN。例如:

SELECT 
  child.member_id _ch_mid, 
  parent.member_id _p_mid 
FROM 
   tbl child LEFT JOIN tbl parent 
   ON(child.parent_member_id = parent.member_id)