I am having a query problem Here is my db structure PK_ID,title,parent 1,Propery,0 2,Services,0 3,Rent,1 4,House,3 Now I have PK_ID=4 and want to find its name which is House, its parent name which is Rent and then its parent which is Property I don't know how to make query for it? any idea thank you -dizyn
Most likely you will need to do implement a do while loop with PHP. http://us2.php.net/do.while Keep querying for the title using the parent field. SELECT title FROM db WHERE parent = ? I hope this helps you get on the right track.
To do it in a single query you need to change your table structure. See: http://dev.mysql.com/tech-resources/articles/hierarchical-data.html
If you want a set hierarchy of 2 levels then you can do it with the current structure and a single query - ignore people saying to use a PHP loop - there are some good PHP programmers here but many are terrible at SQL and continuous trips to the sql server is very inefficient. If you are wanting nth level structure then you will be better off restructuring the db to make more efficient queries