Problem:
3 tables: tbl_product, tbl_soldproducts, tbl_purchasedetail
tbl_product has a primary key of prod_no which is foreign key on each of the 2 table.
tbl_products
tbl_purchasedetail
tbl_soldproducts

In tbl_soldproducts I have column item_sold describe how many items sold for this product per transaction. In tbl_purchasedetail I have qty_purchase which describe as number of item purchase per transaction. I need to count the total item sold and purchase per product. Ex output:
Prod no | item_sold |qty_purchase | left
1 | 23 | 25 | 2
2 | 1 | 10 | 9
My current code which display wrong output:
SELECT TP.prod_no,TP.barcode,TP.prod_name, COUNT(TS.qty) as num_sold,
COUNT(TPS.qty_delivered) as num_delivered
FROM tbl_product AS TP
LEFT JOIN tbl_soldproducts AS TS
on (TS.prod_no = TP.prod_no)
LEFT JOIN tbl_purchasedetail AS TPS
on (TPS.prod_no = TP.prod_no)
group by TS.prod_no
ORDER BY TP.prod_name