存储从多个表中检索数据的过程?

时间:2012-07-23 13:52:37

标签: sql-server-2008 stored-procedures

我有五张看起来像这样的表

BugHistory             
--------------------   
BugHistoryID
BugId
EmployeeId                  

Bugs
--------------------
BugID
Description
ProjectID
PriorityID

Employee                    
-------------------      
EmployeeId
EmployeeName   

Projects                
-------------------     
ProjectID
ProjectName    

Priority
-------------------
PriorityID
PriorityName

我想从所有表中检索数据,使BugHistory成为主表。

我需要[Description],[ProjectName],[PriorityName]来自其余四个表中EmployeeID为关键字的表。

应根据BugHistory表中的EmployeeID检索数据;我将根据登录获取EmployeeID。任何人都可以帮我写这个查询吗?

1 个答案:

答案 0 :(得分:4)

这应该可以满足您的需求。

SELECT b.Description
    , pj.ProjectName
    , pr.PriorityName
FROM BugHistory bh
INNER JOIN Employee e
     ON bh.EmployeeId = e.EmployeeId
INNER JOIN Bugs b
     ON bh.BugId = b.BugId
INNER JOIN Project pj
    ON b.ProjectId = pj.ProjectId
INNER JOIN Priority pr
    ON b.PriorityId = pr.PriorityId