Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
3 Table SQL Join
Message
De
07/12/1999 19:05:38
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00300033
Message ID:
00300115
Vues:
28
>>
>>For the tables described below:
>>
>>    Table A (ForeignKey, Identifier, DataA)
>>        : Primary Key = ForeignKey+Identifier
>>
>>    Table B (ForeignKey, Identifier, Filter, DataB)
>>        : Primary Key = ForeignKey+Identifier
>>
>>    Table C (Identifier, Filter, DataC)
>>        : Primary Key = Identifier
>>
>>Desired query result:
>>
>>    For ForeignKey = a single value and Filter = a single value
>>
>>    Result Table R (Identifier, DataB, DataC)
>>
>>    Where there is exactly one row for each Identifier in Table A,
>>    even if there are no matching rows in Table B or Table C.
>>
>>What is the SQL Select Statement using SQL 95 Join syntax to generate
>>the desired result?
>>
>>
>
>You would do a left join on Tables B and C.
>
>... from TableA ;
>Left Join TableB ;
>on TableA.Identifier = TableB.Identifier ;
>Left Join TableC ;
>on TableA.Identifier = TableC.Identifier ;
>group by TableA.Identifier
>
>The GROUP BY is needed to return exactly 1 row per identifier in TableA

OOPS! Please note the addition of the Filter columns for TableB and TableC.

The following code returns 2 rows, but I desire 5 rows (one row per row of TableA).
select 0
create table TableA free (FKey i, Ident i, DataA c(10))

insert into TableA (FKey, Ident, DataA) ;
    values (1, 1, "rec1")
insert into TableA (FKey, Ident, DataA) ;
    values (1, 3, "rec2")
insert into TableA (FKey, Ident, DataA) ;
    values (2, 1, "rec3")
insert into TableA (FKey, Ident, DataA) ;
    values (2, 2, "rec4")
insert into TableA (FKey, Ident, DataA) ;
    values (2, 3, "rec5")
    
select 0
create table TableB free (FKey i, Ident i, FiltB i, DataB c(10))

insert into TableB (FKey, Ident, FiltB, DataB) ;
    values (1, 1, 1, "rec1")
insert into TableB (FKey, Ident, FiltB, DataB) ;
    values (1, 1, 3, "rec2")
insert into TableB (FKey, Ident, FiltB, DataB) ;
    values (2, 1, 1, "rec3")
insert into TableB (FKey, Ident, FiltB, DataB) ;
    values (2, 1, 3, "rec4")
insert into TableB (FKey, Ident, FiltB, DataB) ;
    values (2, 3, 1, "rec5")
insert into TableB (FKey, Ident, FiltB, DataB) ;
    values (2, 3, 3, "rec6")

select 0
create table TableC (Ident i, FiltC i, DataC c(10))

for j = 1 to 5
    for i = 1 to 5
       insert into TableC (Ident, FiltC, DataC) ;
           values (i, j, "Def[" + transform(i) + ":" + transform(i) + "]")
    endfor
endfor

select TableA.*, TableB.*, TableC.*;
 from TableA ;
 Left Outer Join TableB ;
 on TableA.Ident = TableB.Ident ;
 and TableA.FKey = TableB.FKey ;
 Left Outer Join TableC ;
 on TableA.Ident = TableC.Ident ;
 where TableB.FiltB = 1 ;
 and TableC.FiltC = 1 ;
 group by TableA.Ident
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform