SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question any better way to select like this?

    Hi,

    I have a category_table with the fields like below:

    id
    category_name
    parent


    To get the Category Name and its parent name, we can use the simple but bad way like below:

    $query=mysql_query("Select * from category_table where category_name like '%tech%'");
    while($row=mysql_fetch_array($query)){

    $query2=mysql_query(""Select * from category_table where parent ='".$row['id']."'");

    }
    May I know how to get the category name with its parent name in one query?
    I need help

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT
      t1.id,
      t1.category_name AS `category_name`,
      t2.category_name AS `parent_name`
    FROM
      category_table t1
    LEFT OUTER JOIN
      category_table t2
    ON
      t1.parent = t2.id

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by Dan Grossman View Post
    Code:
    SELECT
      t1.id,
      t1.category_name AS `category_name`,
      t2.category_name AS `parent_name`
    FROM
      category_table t1
    LEFT OUTER JOIN
      category_table t2
    ON
      t1.parent = t2.id
    Thanks Dan Grossman, I'll do further study on this part!

    Thanks for sharing!
    I need help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •