用于检索类别路径的单个递归查询

时间:2015-11-26 17:37:56

标签: mysql recursive-query

我有以下表结构和内容:

+-------------+-----------+------------+
| category_id | parent_id |    name    |
+-------------+-----------+------------+
|          1  |        0  | Test 1     |
|          2  |        1  | Test 1.1   |
|          3  |        2  | Test 1.1.1 |
|          4  |        0  | Test 2     |
|          5  |        0  | Test 3     |
|          6  |        5  | Test 3.1   |
|          7  |        6  | Test 3.1.1 |
+-------------+-----------+------------+

是否可以编写递归查询以返回结果,如下所示:

+-------------+--------------------------------+
| category_id |              path              |
+-------------+--------------------------------+
|           1 | Test 1                         |
|           2 | Test 1 > Test 1.1              |
|           3 | Test 1 > Test 1.1 > Test 1.1.1 |
|           4 | Test 2                         |
|           5 | Test 3                         |
|           6 | Test 3 > Test 3.1              |
|           7 | Test 3 > Test 3.1 > Test 3.1.1 |
+-------------+--------------------------------+

谢谢!

0 个答案:

没有答案