Im currently porting a MySQL db to another DBMS. I encountered the following query with the following structure.
SELECT ... FROM table1 AS tb1
LEFT JOIN Table2 AS tb2
ON tb1.x = tb2.x
AND tb2.y = 2 AND tb2.z = 3 ...
My target DBMS (DB2) “AND tb2.y = 2 AND tb2.z = 3” does not allow the following structure so i moved it to the WHERE clause. Unfortunately after moving it, the query doesn’t seem to return the same rows from MySQL.
In an outer join, when moving predicates from the
ONclause to theWHEREclause, you have to allow for records where the join produces no match, and therefore columns from the outer table will all beNULL.I have no DB2 experience so there may be other subtleties that I’m not aware of, but this is how I would rewrite the query. Checking for a
NULLin the join column is a direct indicator of whether the join found a match.