一週後搜索我不能寫這個查詢請幫助我。 我有3個表購買,selles,東西。 我想第一總和purchase.quantityselles.quantity字段分開,然後減去這兩個柱purchase.quantity selles.quantity然後GROUP BY purchase.scale,stuff.name從不同的表和組減去兩列
我有這3臺
table purchase:
'id', 'invoice_id', 'stuff_id', 'quantity', 'scale', 'price'
table selles:
'id', 'selles_invoice_num', 'stuff_id', 'quantity', 'scale', 'price'
table stuff:
'id', 'name'
我有這樣的查詢,但結果(數量)是錯誤的
SELECT sum(purchase.quantity) - sum(selles.quantity) as quantities,
purchase.scale,
stuff.name ,
stuff.id
FROM purchase,
selles,
stuff
where purchase.stuff_id = selles.stuff_id
and purchase.scale = selles.scale
and purchase.stuff_id = stuff.id
group by purchase.scale,
stuff.name
ORDER BY stuff.name ,
purchase.scale
你是什麼意思'結果(數量)是錯的'? – Blank
注意tnx –
添加一些示例數據(不要太多,不要太少),當前結果和預期結果。 – jarlh