SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jan 2012
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying grand children in parent-child table

    I have a database table that lists animals in a parent-child relationship, like this:

    TAXON | PARENT
    Mammalia | Vertebrata
    Carnivora | Mammalia
    Canidae | Carnivora
    Canis | Canidae
    Canis_lupus | Canis

    The query below displays a particular taxon's children. For example, on a family page (e.g. Canidae), it will display that family's genera (e.g. Canis). And if I display it on the genus page (Canis), it displays that genus' children, or species (e.g. Canis_lupus).

    Code:
    $Children = mysql_query ("SELECT L.N, L.Taxon, L.Parent, L.Rank
     FROM gz_life AS L
     WHERE L.Parent = '$MyURL'");
    I'd like to know if there's a way to skip a generation, so that displaying the query on a family page would display that family's GRAND CHILDREN, or species.

    TAXON | PARENT
    Mammalia | Vertebrata
    Carnivora | Mammalia
    Canidae | Carnivora
    Canis | Canidae (Skip the children)
    Canis_lupus | Canis (Display the grand children instead.)

    I figured out how to do the opposite - display grandparents and great grandparents. But I don't know how to do the opposite.

    Thanks.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Using a JOIN:
    Code:
    SELECT L2.N, L2.Taxon, L1.Parent, L2.Rank
    FROM gz_life AS L1
    INNER JOIN gz_life AS L2
    ON L1.Taxon = L2.Parent
    WHERE L1.Parent = '$MyURL'

  3. #3
    SitePoint Addict
    Join Date
    Jan 2012
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, that's a cool trick; thanks!

    (Maybe I shouldn't call it a "trick," but you know what I mean. )


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
  •