开发者

SQL need to return data even if specific where statement isnt matched

I've built an extension to a employment application where we can easily add new questions to the form. I need to query to match on both which job they applied to and what application ID it is. I need to return both the answer and the question, the problem is it will return both if both are defined, but I need it to return the question, even if the application isn't defined, but right now it'll only return the question if the answer is defined.

Please help!

Code: (Note the where uses a Coldfusion variable, so nothing out of the normal)

    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.questionDe开发者_StackOverflow中文版finitionID = 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 below:

SQL need to return data even if specific where statement isnt matched


I would use left across. I'm not sure you used FULL JOIN intentionally except in an attempt to get better results, right? Also please learn to use aliases for tables in your joins - people who have to read your code will thank you, I promise.

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#" />


Simply you have to use left outer join there...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜