2010-01-26 7 views
0

j'ai 3 tabels dans ma base de données SQL Server avec les informations suivantes:Comment rejoindre plusieurs tabels dans les scripts de requête

tabel:  TeacherInfo 
columns:  tID,  tName,  tAge,  tLocationId 
values  1   Abel  53   1 
      2   Bob  23   2 
      3   Apaul  24   2 

tabel:  LocationInfo 
columns: lId, lDesc 
values:  1  Street A 
      2  Street B 

tabel:  CourseInfo 
columns: cId, cName, masterTeacherId, assistTeacherId 
values:  1  Maths   1     2 
      2  English  1     3 

Ce que je besoin est un script de requête SQL qui peut renvoyer les valeurs suivantes,

couseName, masterName, masterAge, masterLocation, assistName, assistAge, assistLocation 

Maths  Abel  53  Street A Bob  23  Street B 
English  Abel  53  Street A Apaul  24  Street B 

Alors, comment écrire le script SQL? Merci d'avance!

+0

Sons comme les devoirs un peu ... – Kosi2801

+0

Il est vraiment pas devoirs :-) –

Répondre

1

Vous devez Alais les tables différemment dans la clause FROM:

SELECT CourseInfo.cName as courseName, Mst.tName as masterName, 
     Mst.tAge as masterAge, MstLoc.lDesc as masterLocation, 
     Ass.tName as assistName, Ass.tAge as assistAge, AssLoc.lDesc as 
assistLocation 

FROM CourseInfo 
    join TeacherInfo Mst on masterTeacherId = Mst.tID 
    join LocationInfo MstLoc on Mst.tLocationId = MstLoc.lId 
    join TeacherInfo Ass on assistTeacherId = Ass.tID 
    join LocationInfo AssLoc on Ass.tLocationId = AssLoc.lId 
1
select cName as couseName, a.tName as masterName, a.tAge as masterAge, 
a.lDesc as masterLocation, c.tName as assistName, c.tAge as assistAge, 
d.lDesc as assistLocation 
from CourseInfo 
join TeacherInfo a on masterTeacherId = a.tID 
join LocationInfo b on a.tLocationId = b.lId 
join TeacherInfo c on assistTeacherId = c.tID 
join LocationInfo d on c.tLocationId = d.lId 
Questions connexes