SQL Query连接问题

时间:2014-01-21 05:23:40

标签: sql sql-server join

我正在使用SQL Server 2005。

我在执行此查询时遇到了一些问题。

我的代码是

ALTER PROCEDURE [dbo].[Get]           
  @ApplicantID  int            

AS                      
BEGIN 
    SELECT 
        isnull(M_EvalApplicationStatuses.EvalApplicationStatus,'')        EvalApplicationStatus,
        isnull(M_Users.CompletionMailSent,'') MailSent,
        isnull(APP_Applications.FirstName,'') FirstName,
        isnull(APP_Applications.LastName,'') LastName,
        isnull(M_Users.UserName,'') UserName,
        isnull(APP_Applications.DocTrackingGenComment,'') DocTrackingGenComment

FROM
        APP_Applications
    left outer join
        M_Users
    ON
        APP_Applications.UserID = M_Users.UserID                        
    left outer join
        M_EvalApplicationStatuses
    ON
        APP_Applications.Status = M_EvalApplicationStatuses.EvalApplicationStatusID and M_EvalApplicationStatuses.Status = 1                    

WHERE
        ApplicantID =@ApplicantID
END 

这现在完美无缺。但我想从另一个表中获取数据,所以我只是将该表连接到此查询。我在这里找到了这个问题。我的新表名是[APP_DocumentTracking]以及我用来检索数据的以下查询。

SELECT DISTINCT 
    isnull(APP_DocumentTracking.Date,'') Date,
    isnull(APP_DocumentTracking.IntervTime,'') IntervTime,
    isnull(APP_DocumentTracking.Telephoneinterview,'') Telephoneinterview
FROM [APP_DocumentTracking] 
where APP_DocumentTracking.ApplicantID = @ApplicantID

这两个查询分别正常工作......但我想加入这两个查询,结果将在一个表中获取。我怎样才能做到这一点?请帮助我

3 个答案:

答案 0 :(得分:0)

只需加入第一个和第二个子查询。请在SqlFiddle中更新结构或在此处更新。然后很容易给出答案......

这是代码

ALTER PROCEDURE [dbo].[Get]           
  @ApplicantID  int            

AS                      
BEGIN 
    SELECT 
        isnull(M_EvalApplicationStatuses.EvalApplicationStatus,'')        EvalApplicationStatus,
        isnull(M_Users.CompletionMailSent,'') MailSent,
        isnull(APP_Applications.FirstName,'') FirstName,
        isnull(APP_Applications.LastName,'') LastName,
        isnull(M_Users.UserName,'') UserName,
        isnull(APP_Applications.DocTrackingGenComment,'') DocTrackingGenComment 
        ,Trace.*

FROM
        APP_Applications
    left outer join
        M_Users
    ON
        APP_Applications.UserID = M_Users.UserID                        
    left outer join
        M_EvalApplicationStatuses
    ON
        APP_Applications.Status = M_EvalApplicationStatuses.EvalApplicationStatusID and M_EvalApplicationStatuses.Status = 1                    


LEFT JOIN 

(SELECT DISTINCT 
    isnull(APP_DocumentTracking.Date,'') Date,
    isnull(APP_DocumentTracking.IntervTime,'') IntervTime,
    isnull(APP_DocumentTracking.Telephoneinterview,'') Telephoneinterview
 FROM [APP_DocumentTracking] 
 where APP_DocumentTracking.ApplicantID = @ApplicantID
) AS Trace 

ON APP_Applications.ApplicantID = Trace.ApplicantID

WHERE APP_Applications.ApplicantID =@ApplicantID  

END

干杯。

答案 1 :(得分:0)

ALTER PROCEDURE [dbo].[Get]           
  @ApplicantID  int            

AS                      
BEGIN 
    SELECT 
        isnull(M_EvalApplicationStatuses.EvalApplicationStatus,'')        EvalApplicationStatus,
        isnull(M_Users.CompletionMailSent,'') MailSent,
        isnull(APP_Applications.FirstName,'') FirstName,
        isnull(APP_Applications.LastName,'') LastName,
        isnull(M_Users.UserName,'') UserName,
        isnull(APP_Applications.DocTrackingGenComment,'') DocTrackingGenComment

FROM APP_Applications
    left outer join M_Users
    ON APP_Applications.UserID = M_Users.UserID                        
    left outer join
        M_EvalApplicationStatuses
    ON
        APP_Applications.Status = M_EvalApplicationStatuses.EvalApplicationStatusID and M_EvalApplicationStatuses.Status = 1

    left join
        (
        SELECT DISTINCT 
            isnull(APP_DocumentTracking.Date,'') Date,
            isnull(APP_DocumentTracking.IntervTime,'') IntervTime,
            isnull(APP_DocumentTracking.Telephoneinterview,'') Telephoneinterview
            APP_DocumentTracking.ApplicantID
        FROM [APP_DocumentTracking] 
        where APP_DocumentTracking.ApplicantID = @ApplicantID
        ) AliasSubquery
        on 1=1

WHERE
        ApplicantID =@ApplicantID
END

答案 2 :(得分:0)

根据你在sarathkumar的回答中的评论,问题是不明确的列名'ApplicantID'。尝试以下问题。我已从您的评论中更新了where clause

left outer join [APP_DocumentTracking] on [APP_DocumentTracking].ApplicantID=APP_Applications.ApplicantID 
WHERE [APP_DocumentTracking].ApplicantID =@ApplicantID END