即使特定的where语句不匹配,SQL也需要返回数据

时间:2011-09-26 15:40:46

标签: sql sql-server-2008 coldfusion

我已经为就业申请建立了一个扩展,我们可以轻松地向表单添加新问题。我需要查询以匹配它们应用于哪个作业以及它是什么应用程序ID。我需要返回答案和问题,问题是如果两者都被定义,它将返回两者,但是我需要它来返回问题,即使应用程序没有定义,但是现在它只会返回问题是答案是否定义。

请帮忙!

代码:(注意哪里使用Coldfusion变量,所以没有什么不正常)

    SELECT     
        dbo.mod_employmentAppQuestionAnswers.questionID 
        ,dbo.mod_employmentAppQuestionAnswers.questionDefinitionID 
        ,dbo.mod_employmentAppQuestionAnswers.AppID 
        ,dbo.mod_employmentAppQuestionAnswers.questionText 
        ,dbo.mod_employmentAppQuestionAnswers.questionDate1 
        ,dbo.mod_employmentAppQuestionAnswers.questionDate2 
        ,dbo.mod_employmentAppQuestionAnswers.questionBit 
        ,dbo.mod_employmentAppQuestionDefinitions.definitionID 
        ,dbo.mod_employmentAppQuestionDefinitions.jobTitleID 
        ,dbo.mod_employmentAppQuestionDefinitions.title AS QuestionTitle 
        ,dbo.mod_employmentAppQuestionDefinitions.questionTypeID 
        ,dbo.mod_employmentAppQuestionDefinitions.description 
        ,dbo.mod_employmentAppQuestionDefinitions.isActive 
        ,dbo.mod_employmentAppJobTitles.title AS JobTitle 
        ,dbo.mod_employmentAppQuestionTypes.type AS QuestionType    
    FROM dbo.mod_employmentAppQuestionAnswers 
        FULL JOIN dbo.mod_employmentAppQuestionDefinitions 
            ON dbo.mod_employmentAppQuestionAnswers.questionDefinitionID = dbo.mod_employmentAppQuestionDefinitions.definitionID 
        INNER JOIN dbo.mod_employmentAppJobTitles 
            ON dbo.mod_employmentAppQuestionDefinitions.jobTitleID = dbo.mod_employmentAppJobTitles.jobTitleID 
        LEFT JOIN dbo.mod_employmentAppQuestionTypes 
            ON dbo.mod_employmentAppQuestionDefinitions.questionTypeID = dbo.mod_employmentAppQuestionTypes.questionTypeID
    WHERE   
        (dbo.mod_employmentAppQuestionDefinitions.jobTitleID = 
                <cfqueryparam cfsqltype="cf_sql_integer" value="#jobTitleID#" />) AND
        (dbo.mod_employmentAppQuestionAnswers.AppID = 
                <cfqueryparam cfsqltype="cf_sql_integer" value="#applicationID#" />)   

下面的数据库设计: Database design

2 个答案:

答案 0 :(得分:3)

我会用左手穿过。我不确定你故意使用FULL JOIN除了试图获得更好的结果,对吗?另外,请学习在联接中使用别名来表示 - 必须阅读代码的人会感谢你,我保证。

SELECT     
    a.questionID 
    ,a.questionDefinitionID 
    ,a.AppID 
    ,a.questionText 
    ,a.questionDate1 
    ,a.questionDate2 
    ,a.questionBit 
    ,d.definitionID 
    ,d.jobTitleID 
    ,d.title AS QuestionTitle 
    ,d.questionTypeID 
    ,d.description 
    ,d.isActive 
    ,t.title AS JobTitle 
    ,qt.type AS QuestionType    
FROM
    dbo.mod_employmentAppQuestionDefinitions AS d
LEFT OUTER JOIN 
    dbo.mod_employmentAppQuestionAnswers AS a 
    ON a.questionDefinitionID = d.definitionID 
    AND a.AppID = <cfqueryparam cfsqltype="cf_sql_integer" value="#applicationID#" />
LEFT OUTER JOIN
    dbo.mod_employmentAppJobTitles AS t
    ON d.jobTitleID = t.jobTitleID 
LEFT OUTER JOIN 
    dbo.mod_employmentAppQuestionTypes AS qt
    ON d.questionTypeID = qt.questionTypeID
WHERE   
    d.jobTitleID = <cfqueryparam cfsqltype="cf_sql_integer" value="#jobTitleID#" />

答案 1 :(得分:-1)

只需要在那里使用左外连接......