I have an table structure like this
mysql> SELECT id, name, parent_id FROM categories;
+-------+------------+-----------+
| id | name | parent_id |
+-------+------------+-----------+
| 15790 | Test | 0 |
| 15791 | Test2 | 0 |
| 16079 | Subtest | 15790 |
| 16080 | Subtest 2 | 15790 |
| 16081 | Subsubtest | 16079 |
+-------+------------+-----------+
Now I want to look up the parent for every children and sibling and give it back in the right order for deletion.
So my output in this case would be:
Array
(
16081,
16080,
16079,
15791,
15790
)
I can’t delete just by reversing the parent ids, because this should be solid walking back the tree.
Also I am not able/allowed to change the structure of the table. So building kind of an index is necessary.
Assuming you don’t have access to
TRUNCATE,SET(so you could doSET FOREIGN_KEY_CHECKS=0;),ALTER, etc. etc., and absolutely must use a script:Since the question is tagged with
php, this should do the trick: