我的数据库中有两个表。
我想创建查询以显示余额,如:
我想:
答案 0 :(得分:1)
经过测试和工作:
SELECT inventory.inv_id, inventory.item, inventory.qty,
COALESCE(SUM( `order`.qty ) , 0 ) AS sum_qty,
COALESCE((inventory.qty - SUM( `order`.qty)) , 0) AS balance
FROM inventory
LEFT JOIN `order` ON inventory.inv_id = `order`.inv_id
GROUP BY `inventory`.inv_id
ORDER BY inventory.inv_id ASC