web-dev-qa-db-fra.com

SQL joint interne 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 universitaire. J'ai un autre tableau qui énumère les préférences de salle (3 d'entre elles) pour chaque étudiant. 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 une troisième table (n'a pas conçu cette base de données ...).

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

005 serait la HallID. Alors maintenant, je veux faire correspondre cette HallID à une troisième table, où cette table contient une HallID et HallName.

Tellement, je veux que mon résultat soit comme ...

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

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
276
Bob Sanders

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

En fonction de votre demande pour plusieurs salles, vous pouvez le faire de cette façon. Vous venez de rejoindre plusieurs fois sur votre table Hall pour chaque ID de salle préféré:

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
435
Taryn
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
41
Lomorng

Si vous avez 3 tables avec la même ID à joindre, je pense que ce serait comme ça

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

Il suffit de remplacer * par ce que vous voulez obtenir des tables.

36
aquatorrent
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
4
Khurram Basharat

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 et voilá :)

2
aF.

Il y a eu beaucoup de réponses, mais la leçon générale semble être que vous pouvez utiliser plusieurs JOINS dans une clause where; De plus, techonthenet.com (mon patron me l'a recommandé, c'est ce que j'ai trouvé) propose de bons tutoriels SQL si vous avez déjà une autre question et que vous voulez juste essayer de la comprendre. 

SELECT table1.column1
FROM table1
WHERE table1 > 0 (or whatever you want to specify)
INNER JOIN table1 
ON table1.column1 = table2.column1
1
Nathan

Cette requête est correcte pour la table join 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é premier tableau . rapport deuxième tableau .. naissance troisième tableau

0
Sri Siva

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
0
select products.product_id, product_name, price, created_at, image_name, categories.category_id, category_name,brands.brand_id, brand_name 
FROM products INNER JOIN categories USING (category_id) INNER JOIN brands USING(brand_id)
0
Eknojor Bd
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
0
p.ajay
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;
0
ashu