mysql> select * from categories order by path, name; +----+---------------+--------------------------+------------+------------+ | id | name | path | created_at | updated_at | +----+---------------+--------------------------+------------+------------+ | 2 | Cars | / | NULL | NULL | | 1 | Entertainment | / | NULL | NULL | | 3 | Gasoline | /Cars | NULL | NULL | | 6 | Parts | /Cars | NULL | NULL | | 4 | Green Gas | /Cars/Gasoline | NULL | NULL | | 8 | Light Gas | /Cars/Gasoline | NULL | NULL | | 5 | Soja Based | /Cars/Gasoline/Green Gas | NULL | NULL | | 7 | Pieces | /Cars/Parts | NULL | NULL | +----+---------------+--------------------------+------------+------------+ My issue is, I would like to have this (Please note the difference for pleaces having path = /), actually Ineed a "tree": +----+---------------+--------------------------+------------+------------+ | id | name | path | created_at | updated_at | +----+---------------+--------------------------+------------+------------+ | 2 | Cars | / | NULL | NULL | | 3 | Gasoline | /Cars | NULL | NULL | | 6 | Parts | /Cars | NULL | NULL | | 4 | Green Gas | /Cars/Gasoline | NULL | NULL | | 8 | Light Gas | /Cars/Gasoline | NULL | NULL | | 5 | Soja Based | /Cars/Gasoline/Green Gas | NULL | NULL | | 7 | Pieces | /Cars/Parts | NULL | NULL | | 1 | Entertainment | / | NULL | NULL | +----+---------------+--------------------------+------------+------------+