Not getting all records from Table A in left join

doub1ejack

I have a table of Quality Assurance statuses for recipes and want to select all 'discard' records, including info from 'perfect' statuses where they overlap. However, I'm only getting the intersection and I want all 'discard' records.

I want to perform a left join that will give me all 'discard' rows from the recipe_qa table, joined with any 'perfect' rows.

select *
from recipe_qa as bad
left join recipe_qa as good on good.id = bad.id
where bad.type = 'discard'
and good.type = 'perfect'

The query above is returning rows where there is a 'perfect' and a 'discard' record (24 rows) for the id. What I want is a query that will give me all the 'discard' rows (76 rows), with either the 'perfect' id or a null where there is no corresponding row.

Here's a fiddle: http://sqlfiddle.com/#!2/faa49/4

What am I doing wrong?

xQbert

Put simply, your where clause eliminates them.

When working with left (or outer) joins you have to consider when the data limit is imposed vs when the Cartesian is created.

Say you want all records from one table (A) and only those that match in the other (B). When the join is executed, NULL values will be present in B (unmatched records to A). Adding a limiting criteria (where clause) against B fields, will in-fact eliminate records you want from A; as the where clause executes AFTER the join. This has the same effect as if you started with an INNER JOIN! (in this case good.type = 'perfect' will eliminate all those records where bad.type = 'discard' because when good.id doesn't exist for bad.id, good.type will be null, not 'perfect'; thus the where eliminates such records)

This situation can be avoided simply by moving the limiting criteria on the B table to the join when using outer joins. This way the Cartesian is generated AFTER the limit has been imposed ensuring the "All Records" from table A remains all records. If not on the join, as you've seen, the limit is imposed after the Cartesian and thus null values are removed, and you no longer get "All Records" thus the LEFT join is negated. It's as if you were doing an INNER join in the first place. This is why doing an OR statement to return nulls and the value also works but only if it is a NOT NULL column (or type of null has no semantic meaning) as LC points out in comments below.

In this case your where clause of good.type is eliminating the results of the left join so either add the criteria to the join which forces the limits before the Cartesian is generated (allowing the nulls to exist)

select *
from recipe_qa as bad
left join recipe_qa as good 
  on good.id = bad.id
 and good.type = 'perfect'
where bad.type = 'discard'

http://sqlfiddle.com/#!2/faa49/8/0

OR use an is null condition to not exclude records from left join. This has some risks indicted in the below comments.

select *
from recipe_qa as bad
left join recipe_qa as good 
  on good.id = bad.id
where bad.type = 'discard'
 and (good.type = 'perfect' or good.type is null)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

LEFT JOIN does not return all the records from the left side table

From Dev

Linq - Inner Join not retrieving all records from left table

From Dev

Linq - Inner Join not retrieving all records from left table

From Dev

SQL server left join not returning expected records from left table

From Dev

Retriving all records from left table and matching records from right

From Dev

Get all records with left join even there's no record on table B

From Dev

Getting all records from database to Vaadin Table

From Dev

unable to get records from left join in 0:m relationship table

From Dev

unable to get records from left join in 0:m relationship table

From Dev

LEFT join not returning all rows from LEFT table

From Dev

Get all from left table event if there is nothing in join table

From Dev

Getting minimum price from other table using left join

From Dev

MySQL returning all the records from the left table excluding the records that have a match in the right table

From Dev

Left join is not returning all rows from other table

From Dev

How to use left join to take all data from master table?

From Dev

Left join either returns all records or none

From Dev

getting all the records from table -Parse.com

From Dev

Oracle, LEFT OUTER JOIN not returning all rows from left table, instead behaving like INNER JOIN

From Dev

Left Join not returning all rows in left table

From Dev

Return records from table 1 left join table 2 even if where condition fails

From Dev

Mysql left join, get all right table columns and 2 columns from left table based on left table max id

From Dev

Not all strings from left join

From Dev

LEFT JOIN - fetching all data from left table with no matches in the right one

From Dev

Laravel 2 table join with all from left table merged with members of the right table with a given FK value

From Dev

Select all from Table A, one column from Table B - LEFT JOIN

From Dev

left join query does not return left table records if right table does not have records

From Dev

Query to join two tables by mapping third table without returning all records from third table in Oracle

From Dev

LEFT JOIN to return all results of of first table

From Dev

Getting SQLite to delete all records in a table

Related Related

  1. 1

    LEFT JOIN does not return all the records from the left side table

  2. 2

    Linq - Inner Join not retrieving all records from left table

  3. 3

    Linq - Inner Join not retrieving all records from left table

  4. 4

    SQL server left join not returning expected records from left table

  5. 5

    Retriving all records from left table and matching records from right

  6. 6

    Get all records with left join even there's no record on table B

  7. 7

    Getting all records from database to Vaadin Table

  8. 8

    unable to get records from left join in 0:m relationship table

  9. 9

    unable to get records from left join in 0:m relationship table

  10. 10

    LEFT join not returning all rows from LEFT table

  11. 11

    Get all from left table event if there is nothing in join table

  12. 12

    Getting minimum price from other table using left join

  13. 13

    MySQL returning all the records from the left table excluding the records that have a match in the right table

  14. 14

    Left join is not returning all rows from other table

  15. 15

    How to use left join to take all data from master table?

  16. 16

    Left join either returns all records or none

  17. 17

    getting all the records from table -Parse.com

  18. 18

    Oracle, LEFT OUTER JOIN not returning all rows from left table, instead behaving like INNER JOIN

  19. 19

    Left Join not returning all rows in left table

  20. 20

    Return records from table 1 left join table 2 even if where condition fails

  21. 21

    Mysql left join, get all right table columns and 2 columns from left table based on left table max id

  22. 22

    Not all strings from left join

  23. 23

    LEFT JOIN - fetching all data from left table with no matches in the right one

  24. 24

    Laravel 2 table join with all from left table merged with members of the right table with a given FK value

  25. 25

    Select all from Table A, one column from Table B - LEFT JOIN

  26. 26

    left join query does not return left table records if right table does not have records

  27. 27

    Query to join two tables by mapping third table without returning all records from third table in Oracle

  28. 28

    LEFT JOIN to return all results of of first table

  29. 29

    Getting SQLite to delete all records in a table

HotTag

Archive