#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Child and Parent Query


    I have a query : How can i select catid,catname,parentname from given below table.

    Table name is : categories
    catid catname parent_id
    1 abc 0
    2 xyz 1
    3 pqr 1

    Kindly help me out . Thanks and Regards .
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2008
    Posts
    1,928
    Rep Power
    378
    Is this homework?
  4. #3
  5. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,262
    Rep Power
    4279
    Originally Posted by cafelatte
    Is this homework?
    yes

    this same problem shows up every year, and i've seen it four times this week
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    No


    Originally Posted by cafelatte
    Is this homework?
    No It is mysql table . And i want to make a query as i mentioned .
  8. #5
  9. Hockey face
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    St. Catharines, Canada
    Posts
    8,147
    Rep Power
    1317
    Then perhaps you should show us what you have tried rather than posting a question that smells like homework... but isn't homework.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Actually i am also confused what will be the answer of this question As i need to pull catid,catname,parentname from given table . I am using left outer join for this. But i dont know it is right or not to use left outer join .
    I made a query like this : select (A.catname) as parentname,B.catid,B.catname from cat as A left outer join cat as B on A.catid=B.parent_id
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2008
    Posts
    1,928
    Rep Power
    378
    That's very good. Now, if only you had used NULL instead of 0 for orphaned elements...

IMN logo majestic logo threadwatch logo seochat tools logo