我试图在视图中连接3个表;情况是这样的:

我有一张表格,上面有申请在这所大学校园居住的学生的信息。我有另一个表,列出了每个学生的大厅偏好(其中3个)。但是每个参数都只是一个ID Number,并且ID Number在第三个表中有一个对应的Hall Name(没有设计这个数据库…)。

基本上,我在表中有INNER JOIN和它们的首选项,以及它们的信息,结果是这样的。

 John Doe | 923423 | Incoming Student | 005

005是HallID。所以现在我想把这个HallID匹配到第三个表,这个表包含HallID和HallName。

所以,我希望结果是这样的。

 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

当前回答

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

其他回答

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

这个查询将为您工作

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

已经有很多回应,但总的教训似乎是,你可以在一个where子句中使用多个join;还有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

如果你有3个具有相同ID的表要连接,我认为它会像这样:

SELECT * FROM table1 a
JOIN table2 b ON a.ID = b.ID
JOIN table3 c ON a.ID = c.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