0

I have the following table:

categoryID categoryName parentID
6 Category6 0
7 Category7 0
12 Category12 6
13 Category13 7

How can I get the following order:

categoryID categoryName parentID
6 Category6 0
12 Category12 6
7 Category7 0
13 Category13 7

That is each "child" category to go under its parent?

I tried with different select and ordering, group by queries, but nothing seems to work.

This question has been marked as duplicate, but it is not. Solution posted on: How to create a MySQL hierarchical recursive query? shows only the tree for selected category, and I want all categories (the whole tree)

Strawberry
  • 33,750
  • 13
  • 40
  • 57
D.B.
  • 89
  • 1
  • 9
  • Build fully qualified paths then use them for sorting. – Akina Jun 02 '21 at 08:02
  • First of all, the difference between "I want all categories (the whole tree)" and "shows only the tree for selected category" is an adjustment of the where condition (e.g. to adjust `where id = 19`). It's fine if you need help with that, but you should at least mention that you tried out those solutions and where you failed - as getting the children is the actual hard part of your problem, and that is covered in the linked question. Nevertheless, as also mentioned there, another really important information is missing from your question: you need to tell us your mysql version! – Solarflare Jun 02 '21 at 09:22
  • Thanks for the instructions, I solve my issue from PHP. – D.B. Jun 02 '21 at 09:43

0 Answers0