I have 4 tables.
CREATE TABLE Branch( ID INT, Name VARCHAR(50) ); INSERT INTO Branch VALUES (1,'A'), (2,'B'); CREATE TABLE Product( ID INT, Name VARCHAR(50) ); INSERT INTO Product VALUES (1,'X'), (2,'Y'); CREATE TABLE StockIn( ID INT, ProductId INT, Quantity INT, BranchId INT ); INSERT INTO StockIn VALUES (1,1,10,1), (2,1,20,1), (3,1,50,2), (4,1,10,2); CREATE TABLE StockOut( ID INT, ProductId INT, Quantity INT, BranchId INT ); INSERT INTO StockOut VALUES (1,1,5,1), (2,1,21,1), (3,1,45,2), (4,1,5,2); Now i want to calculate stock from these (StockIn-StockOut).
by using below query i got stockin and stockout from stock tables by grouping thier branches.
StockIn
select BranchId, ifnull(sum(Quantity),0) Quantity from stockin where productid=1 group by BranchId; StockOut
select BranchId, ifnull(sum(Quantity),0) Quantity from stockout where productid=1 group by BranchId; and i want to show the result like this


