razin223 Posted January 3, 2011 Share Posted January 3, 2011 Table - SELL ------------------------------ | model | quantity | ---------------------------- | C | 500 | | B | 500 | | C | 300 | | B | 200 | ----------------------------- Table - BUY ------------------------------ | model | quantity | ------------------------------- | A | 2000 | | B | 2000 | | C | 2000 | | A | 1000 | ------------------------------- I need to SUM of SELL and BUY both Group By. Then I need to subtract them as (BUY - SELL) so I can have the current status of my stock. Just like this ---------------------------- | model | quantity | ------------------------------- | A | 3000 | | B | 1300 | | C | 1200 | --------------------------------- Now how can I do it?My Mysql version is 5.1 Link to comment https://forums.phpfreaks.com/topic/223279-grouping-by-sum-from-two-table-and-subtracting-them-prob/ Share on other sites More sharing options...
joel24 Posted January 4, 2011 Share Posted January 4, 2011 something along these lines SELECT b.model, SUM(b.quantity) - SUM(s.quantity) AS currentQuantity FROM sell s JOIN buy b ON s.model=b.model GROUP BY b.model Link to comment https://forums.phpfreaks.com/topic/223279-grouping-by-sum-from-two-table-and-subtracting-them-prob/#findComment-1154447 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.