Bug 15675 [QA Followup] - Switch from NOT IN to LEFT JOIN
authorKyle M Hall <kyle@bywatersolutions.com>
Tue, 9 Feb 2016 14:38:57 +0000 (14:38 +0000)
committerBrendan Gallagher <brendan@bywatersolutions.com>
Wed, 2 Mar 2016 03:24:46 +0000 (03:24 +0000)
Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com
installer/data/mysql/atomicupdate/bug_15334.sql

index f133705..89aead4 100644 (file)
@@ -5,7 +5,17 @@ ALTER TABLE accountlines ADD issue_id INT(11) NULL DEFAULT NULL AFTER accountlin
 UPDATE accountlines LEFT JOIN issues USING ( itemnumber, borrowernumber ) SET accounttype = 'F' WHERE accounttype = 'FU' and issues.issue_id IS NULL;
 
 -- Close out any extra not really accruing fines, keep only the latest accring fine
-UPDATE accountlines SET accounttype = 'F' WHERE accountlines_id NOT IN ( SELECT accountlines_id FROM ( SELECT * FROM accountlines WHERE accounttype = 'FU' ORDER BY accountlines_id DESC ) a2 GROUP BY borrowernumber, itemnumber );
+UPDATE accountlines a1
+    LEFT JOIN (SELECT MAX(accountlines_id) AS keeper,
+                      borrowernumber,
+                      itemnumber
+               FROM   accountlines
+               WHERE  accounttype = 'FU'
+               GROUP BY borrowernumber, itemnumber
+              ) a2 USING ( borrowernumber, itemnumber )
+SET    a1.accounttype = 'F'
+WHERE  a1.accounttype = 'FU'
+  AND  a1.accountlines_id != a2.keeper;
 
 -- Update the unclosed fines to add the current issue_id to them
 UPDATE accountlines LEFT JOIN issues USING ( itemnumber ) SET accountlines.issue_id = issues.issue_id WHERE accounttype = 'FU';