I have a categories table that looks like this:
id | name | parent
-----------------------
1 | Toys | 1
2 | Clothing | 1
3 | Kid's Toys | 0
I have another table called category_relationships which looks like this:
id | category_id | parent_id
----------------------------
1 | 3 | 1
I want to have the following output:
Categories:
Toys
- Kid's Toys
Clothing
How to achieve this with one query?
A better/proper/robust answer will probably be create a MySQL PROCEDURE for this, but if your data can fit in these limitations, you can use the below:
This query uses Concat to build a sortable reference so that children of A come after A etc. The names are indented manually using concat and leading spaces.