0

I have the following table structure and content:

+-------------+-----------+------------+
| 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 |
+-------------+-----------+------------+

Would it be possible to write a recursive query to return the result as following:

+-------------+--------------------------------+
| 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 |
+-------------+--------------------------------+

Thank you!

Cosmin
  • 864
  • 3
  • 16
  • 34
  • Possible duplicate of [How to do the Recursive SELECT query in MySQL?](http://stackoverflow.com/questions/16513418/how-to-do-the-recursive-select-query-in-mysql) – Jocelyn Nov 26 '15 at 17:40
  • 1
    Please do not cross-post: http://dba.stackexchange.com/questions/122212/single-query-for-retrieving-a-category-path One question, in one site, is enough. Otherwise, youn are just creating work for moderators - one of the two questions will be eventually closed. – ypercubeᵀᴹ Nov 26 '15 at 18:25
  • @Jocelyn, I'm not quite sure it's a duplicate. I want to output the result in a different manner. – Cosmin Nov 30 '15 at 20:23

0 Answers0