[英]Left Outer Join not returning all records from primary table
When I do a left outer join, I expect to get all the records that the query would return prior to adding the joined table, but it is only returning records that match the joined table (ie: no record for '092387' exists in table 'documentation', so I just want null returned for 'filename' field for that record.) What am I doing wrong? 当我执行左外连接时,我希望获得查询在添加连接表之前返回的所有记录,但它只返回与连接表匹配的记录(即:表中不存在'092387'的记录'文档',所以我只想为该记录的'filename'字段返回null。)我做错了什么?
mysql> 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'
and documentation.status!=3;
+---------------+-----------------+
| ref_docnumber | filename |
+---------------+-----------------+
| SOP-0042 | SOP-0042r39.pdf |
+---------------+-----------------+
1 row in set (0.00 sec)
mysql> select ref_docnumber
from documentation_reference
where documentation_reference.docnumber='TP-036';
+----------------------+
| ref_docnumber |
+----------------------+
| 092387 |
| 1100218B |
| Applicable Item Spec |
| SOP-0042 |
+----------------------+
4 rows in set (0.00 sec)
Your where clause is converting the outer join back into an inner one. 您的where子句将外部联接转换回内部联接。
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
). outer join
保留的非匹配行将全部具有documentation.status
NULL
值,因此您的documentation.status != 3
条件将过滤掉这些(表达式的结果为NULL !=3
unknown
不是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. 请注意, documentation.status != 3
谓词将移入JOIN
条件。
Check your documentation.status!=3
condition...it might be the culprit..it is I think eliminating your expected records. 检查你的documentation.status!=3
条件......它可能是罪魁祸首......我认为是否会消除你的预期记录。
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 - 检查结果,然后运行查询与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
在添加最终条件 - documentation.status!=3
检查它们应该更改的结果
You should probably follow Martin's advice and run his code to get expected results. 您应该遵循Martin的建议并运行他的代码以获得预期的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.