SQL内部联接与3个表?


330

我试图在一个视图中联接3个表;情况如下:

我有一张桌子,其中包含正在申请住在此大学校园的学生的信息。我还有另一个表格,列出每个学生的“大厅偏好设置”(其中有3个)。但是这些首选项只是一个ID号,并且ID号在第三张表中有一个对应的Hall Name(不是设计此数据库...)。

差不多,我INNER JOIN在桌子上看到他们的偏好和他们的信息,结果是...

 John Doe | 923423 | Incoming Student | 005

哪里005会是HallID。所以现在我想将其与HallID第三个表匹配,该表包含一个HallIDHallName

差不多,我希望我的结果像...

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

这是我目前拥有的:

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

Answers:


503

您可以执行以下操作(我猜在表字段上,等等)

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

根据您对多个礼堂的要求,您可以采用这种方式。您只需为每个房间偏好的ID多次加入Hall表:

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

1
这仅适用于一个首选项,但是我要如何对其进行编辑以使其适用于3个首选项?(每个偏好设置一栏)
Bob Sanders

1
@BobSanders刚刚更新了我的答案,然后,如果您不希望HallPref编号仅删除这些列
Taryn

46
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

27
该答案缺乏合理的解释来说明OP如何实现原始目标。
gaige

41

如果您有3个ID要连接的表,我想它将是这样的:

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

只需替换*为您要从表中获取的内容即可。



3

您只需要第二个内部联接即可将ID Number您现在拥有的内部联接链接到ID Number第三个表的联接。此后,更换ID NumberHall Name瞧:)


2
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;

2

已有很多回应,但是总的教训似乎是,您可以在where子句中使用多个JOINS。还有techonthenet.com(我的老板向我推荐了它,这就是我找到它的方式),如果您还有其他问题并且只想尝试解决,那么它也提供了很好的SQL教程。

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

1

这是对具有相同ID的联接3表的正确查询**

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';

员工第一张桌子。报告第二张表。出生第三表


1
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

1

该查询将为您服务

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

1
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)

-5
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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.