MySQL query to order by parent then child places
- by Swanny
I have a table of pages in my database, each page can have a parent as below:
id parent_id title
1 0 Home
2 0 Sitemap
3 0 Products
4 3 Product 1
5 3 Product 2
6 4 Product 1 Review Page
What would be the best MySQL query to select all pages ordered by parent then child then child again if there is more than one level, there will be a maximum of three levels. The above example would produce the desired order:
Home
Sitemap
Products
Product 1
Product 1 Review Page
Product 2