Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SQL Parent-Child-Grandchild...
Message
De
28/02/2001 11:14:43
 
 
À
28/02/2001 10:58:10
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00480544
Message ID:
00480553
Vues:
9
>I need a couple of SQL statements (or statement groups) to select all descendents/ancestors in a Parent-Child table. Single level selects are easy, but I am having trouble with multiple generations.
>
>Thank you for your help.
>
>
>CREATE TABLE tblFamily ;
> (iID I, ;
> iParentID I, ;
> cInfoAndStuff C(20))
>
>
>SQL 1: Select all descendents of iID
SELECT tblParent.iID AS P_iID, ;
    tblParent.iParentID AS P_iParentID, ;
    tblParent.cInfoAndStuff AS P_cInfoAndStuff, ;
    tblChild.iID AS C_iID, ;
    tblChild.cInfoAndStuff AS C_cInfoAndStuff ;
    FROM tblFamily tblParent ;
        INNER JOIN tblFamily tblChild
            ON tblParent.iID == tblChild.iParentID
    ORDER BY tblChild.iID
>SQL 2: Select all ancestors of iID
Based on this structure, each record can only have one ancestor:
SELECT tblChild.iID AS C_iID, ;
    tblChild.iParentID AS C_iParentID, ;
    tblChild.cInfoAndStuff AS C_cInfoAndStuff, ;
    tblParent.cInfoAndStuff AS P_cInfoAndStuff ;
    FROM tblFamily tblChild ;
        INNER JOIN tblFamily tblParent
            ON tblChild.iParentID == tblParent.iID
Hope this helps!
Sylvain Demers
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform