MySQL query to order by parent then child places
Posted
by Swanny
on Stack Overflow
See other posts from Stack Overflow
or by Swanny
Published on 2009-08-15T11:38:18Z
Indexed on
2010/04/10
21:23 UTC
Read the original article
Hit count: 278
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
© Stack Overflow or respective owner