Given the following data and table structure:
id name parent_id
================================
1 food NULL
2 snacks 1
3 cakes 2
4 birthdays 3
I would like to output these rows alongside all of their parent categories. So for example I would like to generate a query to output the data as follows:
id name parent_id parent_1 parent_2 parent_3
===================================================================
1 food NULL NULL NULL NULL
2 snacks 1 1 NULL NULL
3 cakes 2 1 2 NULL
4 birthdays 3 1 2 3
With this, I can easily get the IDs of every parent level of a given category.
I have tried doing this using sub queries but not quite managed to get it right. This is what I have tried:
SELECT id, name, parent_id, parent_id AS _parent_1,
(SELECT parent_id FROM categories WHERE id = _parent_1) AS _parent_2,
(SELECT parent_id FROM categories WHERE id = _parent_2) AS _parent_3
FROM `categories`
EDIT: Based on the feedback, it seems it's going to be somewhat difficult to get the data in the desired format.
Would it at the very least be possible to get all child categories of a given category? So for example for category ID 1, it should output the three categories below it (or four if it will include the given category itself).