373

我正在尝试在一个视图中加入 3 个表;情况如下:

我有一张表格,其中包含申请住在这所大学校园的学生的信息。我有另一个表格,列出了每个学生的 Hall Preferences(其中 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
4

12 回答 12

551

您可以执行以下操作(我猜是表格字段等)

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

根据您对多个大厅的要求,您可以这样做。您只需为每个房间 pref 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
于 2012-04-17T16:59:23.410 回答
56

如果你有 3 个相同的表ID要加入,我认为它会是这样的:

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

只需替换*为您想从表格中获得的内容即可。

于 2012-04-17T16:50:08.120 回答
47
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
于 2013-04-02T01:46:22.077 回答
7
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
于 2015-12-29T11:33:53.940 回答
3

您只需要第二个内部连接,将ID Number您现在拥有ID Number的连接到第三个表的连接。之后,用ID NumberHall Name替换 :)

于 2012-04-17T16:50:52.170 回答
3
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;
于 2017-07-12T06:59:04.663 回答
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
于 2018-12-05T22:14:18.680 回答
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';

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

于 2015-02-03T10:33:05.003 回答
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)
于 2019-05-22T10:35:02.700 回答
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
于 2018-03-05T01:02:36.607 回答
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
于 2018-02-18T03:57:53.800 回答
-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
于 2015-03-25T22:10:06.380 回答