树的深度是无限的。例子:
+----+-------+--------------------+
| id | name | parent_id | value |
+----+-------+--------------------+
| 1 | test1 | | 0 |
| 2 | test2 | 1 | 0 |
| 3 | test3 | 2 | 5 |
| 4 | test4 | 1 | 0 |
| 5 | test5 | 4 | 5 |
| 6 | test6 | 4 | 0 |
| 7 | test7 | 6 | 10 |
+----+-------+--------------------+
我想得到一个孩子的总价值。像这样:
+----+-------+--------------------+
| id | name | parent_id | value |
+----+-------+--------------------+
| 1 | test1 | | 20 | = test2.value + test4.value
| 2 | test2 | 1 | 5 | = test3.value
| 3 | test3 | 2 | 5 |
| 4 | test4 | 1 | 15 | = test5.value + test6.value
| 5 | test5 | 4 | 5 |
| 6 | test6 | 4 | 10 | = test7.value
| 7 | test7 | 6 | 10 |
+----+-------+--------------------+
有什么建议吗?谢谢!