나는이 쿼리가 성공적으로 totstock
선택 SUM은
SELECT p.pid,p.product_name,SUM(s.qty) as totstock
FROM tblproducts p
LEFT JOIN tblstocks s ON s.pid=p.pid
GROUP BY p.pid
얻을하지만 난 내 두 번째 테이블을 결합하려고 할 때 잘못 총 totstock
및 totsales
내가이 쿼리를 가지고 있지만 내가 그것을 생각 얻는다 잘못된
SELECT p.pid,p.product_name,SUM(s.qty) as totstock,SUM(sl.qty) as totsale
FROM tblproducts p
LEFT JOIN tblstocks s ON s.pid=p.pid
LEFT JOIN tbls sl ON sl.pid=p.pid
GROUP BY p.pid
제품입니다 -
pid | product_name
1 | pencil
2 | paper
tblproducts
주식 - tblstocks
pid | qty
1 | 1
1 | 3
1 | 5
판매 - tbls
pid | qty
1 | 2
1 | 1
내가 원하는 결과
pid | name | totstock | totsales
1 | pencil | 9 | 3
2 | paper | NULL | NULL
'totstock'과'totsales'는 제가 원하는 결과보다 큽니다. 나는 두 배 또는 세배로 생각한다 – kashimu