hiprakhar Posted July 18, 2010 Share Posted July 18, 2010 Hi, I have to update tempqty of all available products in the productlist. For this I have to calculate the difference of the Sums of each product purchased and sold. I am writing this query for the same in MySql and MS access but they are not working. In Ms Access it says "must be an updateable query", in MySQL the sum of all the qty is update in the tempqty. This is the query. UPDATE CopyProductBatch AS p SET p.tempqty = (SELECT (a.total - b.total) FROM (SELECT SUM(qty) AS total FROM Inventory AS i, CopyProductBatch AS p WHERE type = 'p' AND i.productbatchcode = p.productbatchcode) a, (SELECT SUM(qty) AS total FROM Inventory AS i, CopyProductBatch AS p WHERE type = 's' AND i.productbatchcode = p.productbatchcode) b) Please help Quote Link to comment https://forums.phpfreaks.com/topic/208093-update-rows-with-the-sums-of-fields-from-other-table/ Share on other sites More sharing options...
Mchl Posted July 18, 2010 Share Posted July 18, 2010 For MySQL UPDATE CopyProductBatch AS p LEFT JOIN (SELECT productbatchcode, SUM(qty) AS total FROM Inventory WHERE type = 'p' GROUP BY productbatchcode) AS a USING (productbatchcode) LEFT JOIN (SELECT productbatchcode, SUM(qty) AS total FROM Inventory WHERE type = 's' GROUP BY productbatchcode) AS b USING (productbatchcode) SET p.tempqty = IFNULL(a.total,0) - IFNULL(b.total,0) Quote Link to comment https://forums.phpfreaks.com/topic/208093-update-rows-with-the-sums-of-fields-from-other-table/#findComment-1087779 Share on other sites More sharing options...
hiprakhar Posted July 18, 2010 Author Share Posted July 18, 2010 Thanks Mchl!! The query works perfect in MySQL! One problem though in MS ACCESS it says "syntax error in update statement" and the cursor goes over "USING". Is there any alias for this that works in MS ACCESS Thanks for the support! Quote Link to comment https://forums.phpfreaks.com/topic/208093-update-rows-with-the-sums-of-fields-from-other-table/#findComment-1087790 Share on other sites More sharing options...
Mchl Posted July 18, 2010 Share Posted July 18, 2010 Sorry, you'll need to find someone who knows SQL flavor used in Access ( you might check MSSQL forums) Quote Link to comment https://forums.phpfreaks.com/topic/208093-update-rows-with-the-sums-of-fields-from-other-table/#findComment-1087793 Share on other sites More sharing options...
hiprakhar Posted July 18, 2010 Author Share Posted July 18, 2010 Thanks anyways for suggesting the query. Your help is deeply appreciated Quote Link to comment https://forums.phpfreaks.com/topic/208093-update-rows-with-the-sums-of-fields-from-other-table/#findComment-1087797 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.