SQL Inner-join avec 3 tables?

J’essaie de joindre 3 tables dans une vue; voici la situation:

J’ai un tableau qui contient des informations sur les étudiants qui demandent à vivre sur ce campus. J’ai un autre tableau qui répertorie les Préférences Hall (3 d’entre eux) pour chaque élève. Mais chacune de ces préférences est simplement un numéro d’identification, et le numéro d’identification a un nom de salle correspondant dans un troisième tableau (n’a pas conçu cette firebase database …).

En gros, j’ai INNER JOIN sur la table avec leurs préférences et leurs informations, le résultat est quelque chose comme …

John Doe | 923423 | Incoming Student | 005 

Où 005 serait le HallID. Donc, maintenant, je veux faire correspondre ce HallID à une troisième table, où cette table contient un HallID et un HallName.

Donc, en gros, je veux que mon résultat soit comme …

  John Doe | 923423 | Incoming Student | Foley Hall <---(INSTEAD OF 005) 

EDIT voici ce que j’ai actuellement

 SELECT s.StudentID, s.FName, s.LName, s.Gender, s.BirthDate, s.Email, r.HallPref1, r.HallPref2, r.HallPref3 FROM dbo.StudentSignUp AS s INNER JOIN RoomSignUp.dbo.Incoming_Applications_Current AS r ON s.StudentID = r.StudentID INNER JOIN HallData.dbo.Halls AS h ON r.HallPref1 = h.HallID 

Vous pouvez faire ce qui suit (j’ai deviné sur des champs de table, etc.)

 SELECT s.studentname , s.studentid , s.studentdesc , h.hallname FROM students s INNER JOIN hallprefs hp on s.studentid = hp.studentid INNER JOIN halls h on hp.hallid = h.hallid 

MODIFIER:

Sur la base de votre demande de plusieurs salles, vous pouvez le faire de cette façon. Vous venez de rejoindre votre table Hall plusieurs fois pour chaque identifiant room pref:

 SELECT s.StudentID , s.FName , s.LName , s.Gender , s.BirthDate , s.Email , r.HallPref1 , h1.hallName as Pref1HallName , r.HallPref2 , h2.hallName as Pref2HallName , r.HallPref3 , h3.hallName as Pref3HallName FROM dbo.StudentSignUp AS s INNER JOIN RoomSignUp.dbo.Incoming_Applications_Current AS r ON s.StudentID = r.StudentID INNER JOIN HallData.dbo.Halls AS h1 ON r.HallPref1 = h1.HallID INNER JOIN HallData.dbo.Halls AS h2 ON r.HallPref2 = h2.HallID INNER JOIN HallData.dbo.Halls AS h3 ON r.HallPref3 = h3.HallID 
 SELECT column_Name1,column_name2,...... From tbl_name1,tbl_name2,tbl_name3 where tbl_name1.column_name = tbl_name2.column_name and tbl_name2.column_name = tbl_name3.column_name 

Si vous avez 3 tables avec le même ID à joindre, je pense que ce serait comme ceci:

 SELECT * FROM table1 a JOIN table2 b ON a.ID = b.ID JOIN table3 c ON a.ID = c.ID 

Remplacez simplement * par ce que vous voulez obtenir des tables.

 SELECT table1.col,table2.col,table3.col FROM table1 INNER JOIN (table2 INNER JOIN table3 ON table3.id=table2.id) ON table1.id(f-key)=table2.id AND //add any additional filters HERE 

Vous avez juste besoin d’une deuxième jointure interne qui relie le ID Number que vous avez maintenant au ID Number de la troisième table. Ensuite, remplacez le ID Number par le Hall Name du Hall Name et voilá 🙂

Ceci est la requête correcte pour rejoindre la table 3 avec le même identifiant **

 select a.empname,a.empsalary,b.workstatus,b.bonus,c.dateofbirth from employee a, Report b,birth c where a.empid=b.empid and a.empid=c.empid and b.empid='103'; 

employé première table. rapport deuxième tableau. troisième table de naissance

 SELECT * FROM PersonAddress a, Person b, PersonAdmin c WHERE a.addressid LIKE '97%' AND b.lastname LIKE 'test%' AND b.genderid IS NOT NULL AND a.partyid = c.partyid AND b.partyid = c.partyid; 
 SELECT A.P_NAME AS [INDIVIDUAL NAME],B.F_DETAIL AS [INDIVIDUAL FEATURE],C.PL_PLACE AS [INDIVIDUAL LOCATION] FROM [dbo].[PEOPLE] A INNER JOIN [dbo].[FEATURE] B ON A.P_FEATURE = B.F_ID INNER JOIN [dbo].[PEOPLE_LOCATION] C ON A.P_LOCATION = C.PL_ID 

Cette requête fonctionnera pour vous

 Select b.id as 'id', u.id as 'freelancer_id', u.name as 'free_lancer_name', p.user_id as 'project_owner', b.price as 'bid_price', b.number_of_days as 'days' from User u, Project p, Bid b where b.user_id = u.id and b.project_id = p.id 
 select empid,empname,managename,[Management ],cityname from employees inner join Managment on employees.manageid = Managment.ManageId inner join CITY on employees.Cityid=CITY.CityId id name managename managment cityname ---------------------------------------- 1 islam hamza it cairo