Topic: Convert WHERE to LEFT JOIN in cust_trans_db SQL

--- sales/includes/db/cust_trans_db.inc    Mon Mar 10 16:17:12 2014
+++ sales/includes/db/cust_trans_db.inc    Wed Dec 25 19:00:22 2013
@@ -377,19 +377,19 @@
             Sum(line.quantity-line.qty_done) AS Outstanding,
             Sum(line.qty_done) AS Done
         FROM "
-         .TB_PREF."sales_orders as sorder, "
-         .TB_PREF."debtor_trans as trans, "
-         .TB_PREF."debtor_trans_details as line, "
-         .TB_PREF."debtors_master as debtor, "
-         .TB_PREF."cust_branch as branch
-            WHERE
-            sorder.order_no = trans.order_ AND
-            trans.debtor_no = debtor.debtor_no
-                AND trans.type = ".ST_CUSTDELIVERY."
-                AND line.debtor_trans_no = trans.trans_no
-                AND line.debtor_trans_type = trans.type
-                AND trans.branch_code = branch.branch_code
-                AND trans.debtor_no = branch.debtor_no ";
+            .TB_PREF."sales_orders as sorder LEFT JOIN "
+            .TB_PREF."debtor_trans as trans 
+                ON (sorder.order_no = trans.order_) LEFT JOIN "
+            .TB_PREF."debtor_trans_details as line 
+                ON (line.debtor_trans_no = trans.trans_no
+                    AND line.debtor_trans_type = trans.type) LEFT JOIN "
+            .TB_PREF."debtors_master as debtor
+                ON (trans.debtor_no = debtor.debtor_no) LEFT JOIN "
+            .TB_PREF."cust_branch as branch 
+                ON (trans.branch_code = branch.branch_code
+                    AND trans.debtor_no = branch.debtor_no)
+        WHERE
+            trans.type = ".ST_CUSTDELIVERY;
 
     if ($_POST['OutstandingOnly'] == true) {
          $sql .= " AND line.qty_done < line.quantity ";

Joe, please include this patch in the core FA repo. It moves most of the WHERE statements to the LEFT JOINs.

Post's attachments

cust_trans_db.patch 1.4 kb, 1 downloads since 2014-03-12 

You don't have the permssions to download the attachments of this post.

Re: Convert WHERE to LEFT JOIN in cust_trans_db SQL

The same as in previous post. LEFT JOIN is used when records joined on the right are optional. This is not the case.
Janusz

Re: Convert WHERE to LEFT JOIN in cust_trans_db SQL

Should we then use INNER JOINS for all joins here?

Thank you for the explanation of the differences.