Left Outer Join not returning all records from primary table

筅森魡賤 提交于 2019-12-05 03:40:59

Your where clause is converting the outer join back into an inner one.

The non matching rows preserved by the outer join will all have NULL values for documentation.status so your documentation.status != 3 condition will filter these back out (The result of the expression NULL !=3 is unknown not true).

To avoid this issue use

select documentation_reference.ref_docnumber,
       documentation.filename
from   documentation_reference
       left outer join documentation
         on ref_docnumber = documentation.docnumber
            and documentation.status != 3
where  documentation_reference.docnumber = 'TP-036'  

Note that the documentation.status != 3 predicate is moved into the JOIN condition.

Check your documentation.status!=3 condition...it might be the culprit..it is I think eliminating your expected records.

Usually to debug this issue run your query in parts, firstly run-

select documentation_reference.ref_docnumber, documentation.filename from documentation_reference left outer join documentation on ref_docnumber=documentation.docnumber

check the results and then run query with the where -

select documentation_reference.ref_docnumber, documentation.filename from documentation_reference left outer join documentation on ref_docnumber=documentation.docnumber
where  documentation_reference.docnumber = 'TP-036'

Check the results they should change once you add the final where condition - documentation.status!=3

You should probably follow Martin's advice and run his code to get expected results.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!