如何在sql查询中使用join?

时间:2013-05-13 16:06:15

标签: sql sql-server join

我正在使用Microsoft SQL服务器,我想使用join但是来自多个表。

这就是我所拥有的

select a.*, b.Position_Name, c.StartDate, c.EndDate--, e.firmName
from NewHire a--, Firms e
join Position b on a.Position_ID = b.Position_ID
join WorkPeriod c on a.HireID = c.HireID-- and c.FirmID = e.FirmID
where a.Archived = 0 
order by a.HireID desc

我希望c.FirmID与e.FirmID匹配,但是我收到了错误

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "a.Position_ID" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "a.HireID" could not be bound.

我已经注释掉了第一个代码块中的三个部分,这会导致错误。有谁知道怎么做?

感谢。

编辑:实际上没关系,我不需要这个问题的帮助。

1 个答案:

答案 0 :(得分:3)

问题是你正在混合JOIN类型,尝试使用相同的JOIN:

select a.*, b.Position_Name, c.StartDate, c.EndDate, e.firmName
from NewHire a
join Position b 
    on a.Position_ID = b.Position_ID
join WorkPeriod c 
    on a.HireID = c.HireID
join Firms e
    on c.FirmID = e.FirmID
where a.Archived = 0 
order by a.HireID desc