Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (i.e: 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?

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)
like image 604
Ted Scheckler Avatar asked Mar 08 '11 17:03

Ted Scheckler


People also ask

Does LEFT join return all rows?

The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. This means that if the ON clause matches 0 (zero) records in the right table; the join will still return a row in the result, but with NULL in each column from the right table.

How many records does LEFT join return?

The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2). The result is 0 records from the right side, if there is no match.

What does left outer join Return?

Left Outer Join returns all the rows from the table on the left and columns of the table on the right is null padded. Left Outer Join retrieves all the rows from both the tables that satisfy the join condition along with the unmatched rows of the left table.


1 Answers

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.

like image 112
Martin Smith Avatar answered Nov 15 '22 19:11

Martin Smith