Reputation: 973
Now ,I have two tables, Location
and q_Location
, location as main table .I write sql left join like this:
SQL1:
SELECT L.ID,QL.*
FROM LOCATION L
LEFT JOIN Q_LOCATION QL ON L.ID=QL.LOCATION_ID
AND L.WAREHOUSE_ID=QL.WAREHOUSE_ID
AND ISNULL(ql.VIRTUAL, 'N') = 'N'
AND ISNULL(ql.PICKABLE, 'y') = 'Y'
where l.warehouse_id='mmc-main
but the result is wrong. if sql statement like this:
SQL2:
SELECT L.ID,QL.*
FROM LOCATION L
LEFT JOIN Q_LOCATION QL ON L.ID=QL.LOCATION_ID
AND L.WAREHOUSE_ID=QL.WAREHOUSE_ID
where l.warehouse_id='mmc-main'
AND ISNULL(ql.VIRTUAL, 'N') = 'N'
AND ISNULL(ql.PICKABLE, 'y') = 'Y'
now The result if correct. I want to know why the first sql is wrong, somebody can help me ? thanks first!
Upvotes: 1
Views: 115
Reputation: 107387
Be careful about applying filters in an OUTER JOIN (left or right) - it is NOT the same as applying the filter in the WHERE clause. Your first query doesn't work as you expect it to, because if the join fails, the LEFT table rows will still be returned, and your additional RIGHT table filters ISNULL(ql.VIRTUAL, 'N') = 'N'
and ISNULL(ql.PICKABLE, 'y') = 'Y'
will be ignored.
See also here: WHERE Clause vs ON when using JOIN
Upvotes: 3