small query problem

Discussion in 'Programming' started by dizyn, Mar 5, 2008.

  1. #1
    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
     
    dizyn, Mar 5, 2008 IP
  2. LeopardAt1

    LeopardAt1 Well-Known Member

    Messages:
    880
    Likes Received:
    126
    Best Answers:
    0
    Trophy Points:
    135
    #2
    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.
     
    LeopardAt1, Mar 5, 2008 IP
  3. lephron

    lephron Active Member

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    lephron, Mar 5, 2008 IP
  4. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    AstarothSolutions, Mar 6, 2008 IP